【发布时间】:2018-05-01 02:44:33
【问题描述】:
tldr
[a-zA-Z\.-] 在 Vim 正则表达式搜索替换中有效,但 [\w\.-] 无效。
我正在搜索的文本:
1 string.here blah blah
24 another-string.here blah.
1523 another-string.goes.here. blah123
期望的输出
string.here
another-string.here
another-string.goes.here
我的问题
为什么会这样:
:%s/\v^\d+\s+([a-zA-z\.-]+)\s+.*/\1/g
但这不是:
:%s/\v^\d+\s+([\w\.-]+)\s+.*/\1/g
E486:找不到模式
:%s/\v^\d+\s+([\w\.-]+)\s+.*/\1/g
两者之间的唯一区别是方括号内的a-zA-Z 与\w。但是\w 等于 a-zA-Z 不(加上此示例文本中没有的其他一些非空白字符)?
我正在使用默认的 vim。未修改。无论 Ubuntu 附带什么。
非 vim 平台
当我尝试使用 atom 文本编辑器而不是 vim 时,两个表达式都可以工作。
搜索:^\d+\s+([a-zA-z\.-]+)\s+.*
替换:$1
当我尝试使用RegExr 时,两个表达式都有效。 (虽然我必须添加多行标签)
我尝试过的其他事情
我的理解是\v 是避免逃离地狱所必需的。我试过没有它:
:%s/^\d\+\s\+\([a-zA-Z\.-]\+\)\s\+.*/\1/g
作品
:%s/^\d\+\s\+\([\w\.-]\+\)\s\+.*/\1/g
不起作用。 (“未找到模式”)
我也尝试添加 m 标志(所以结尾是 /gm),但没有奏效
E488:尾随字符
我也试过不使用^。
:%s/\d\+\s\+\([\w\.-]\+\)\s\+.*/\1/g
E486:找不到模式:
\d\+\s\+\([\w\.-]\+\)\s\+.*
我也尝试过使用\\w 而不是\w。
:%s/\d\+\s\+\([\\w\.-]\+\)\s\+.*/\1/g
E486:找不到模式:
\d\+\s\+\([\\w\.-]\+\)\s\+.*
我也尝试过使用\[ \] 而不是[ ]。
:%s/\d\+\s\+\(\[\\w\.-\]\+\)\s\+.*/\1/g
E486:找不到模式:
\d\+\s\+\(\[\\w\.-\]\+\)\s\+.*
【问题讨论】:
-
vim 不支持字符类中的转义字符