首页 > 解决方案 > 查找除空行之外的任何最后一个字符

问题描述

我需要找到行中的任何最后一个字符,然后添加额外的文本(例如“word”),但不将此规则应用于空行(行中没有字符)。

我的表达方式也([^\n])$适用于空行。

标签: regexssms

解决方案


利用

(\S[^\S\n]*)

替换为$1 word。见证明

解释

--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \S                       non-whitespace (all but \n, \r, \t, \f,
                             and " ")
--------------------------------------------------------------------------------
    [^\S\n]*                 any character except: non-whitespace
                             (all but \n, \r, \t, \f, and " "), '\n'
                             (newline) (0 or more times (matching the
                             most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

推荐阅读