regex - 查找除空行之外的任何最后一个字符
问题描述
我需要找到行中的任何最后一个字符,然后添加额外的文本(例如“word”),但不将此规则应用于空行(行中没有字符)。
我的表达方式也([^\n])$
适用于空行。
解决方案
利用
(\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
推荐阅读
- uwp-xaml - 如何在 UWP 边框中有重复元素?
- php - 下面的问题检查如果没有 %2 == 0 的偶数奇数
- bash - 如何从具有变量作为文件路径的服务器进行 scp?
- firebase - 带有 SetPersistence 的 Firebase SingleValueEventListener
- ubuntu - Ansible 模块无法添加 git-ppa - “W:GPG 错误 - NO_PUBKEY”错误
- node.js - 我如何使用 jest 来模拟一个 promisified 函数
- python - 在类中定义索引的python方法(使用“[]”)
- azure-logic-apps - 无法使用配置文件
- javascript - A chrome extension with Python Flask + Ajax call
- sql - 如何取消透视同一个表中的多个列