简单的正则表达式问题(vim 搜索/替换)
如何指定搜索中的字符串有多个选项? 例如,我想查找以 jspPar
、btn
或 jspAtt
开头且以字母 K
结尾的任何组合代码>.
另外 - 我需要根据原始前缀将其替换为字符串。 例如,如果前缀是 jspPar
,我需要将其替换为字母 P
。 (例如,B
和 A
分别对应 btn
和 jspAtt
)。
How do I specify that there are several options for a string in a search?
For example, I want to find any combination that start with either jspPar
, btn
or jspAtt
that ends with the letter K
.
Also - I need to replace it with a string depending on the original prefix.
for example, if the prefix was jspPar
I need to replace it with the letter P
. (and, let's say, B
and A
for btn
and jspAtt
accordingaly).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是
您要找的吗?
\(jsPar\|btn\|jspAtt\)
表示“此时,匹配这些替代项中的任何一个”,然后[^ \t]*
表示“此时点,匹配任意数量(包括零)的空格或制表符”,而K
当然意味着“此时匹配 K”。对于您添加的问题可以执行如下操作:(
\zs
表示“考虑比赛此时已经开始”,因此只有“K”将被替换。 )但只有当我必须在一次传递中进行替换时,我才会这样做。否则,我只会运行三个
s///
s:给定命令历史记录,输入的次数要少得多,而且也不太可能需要调试,而在指定任何计算时,这总是有可能的。
Is
what you are looking for?
The
\(jsPar\|btn\|jspAtt\)
says “at this point, match any of these alternatives”, then[^ \t]*
says “at this point, match any amount (incl. zero) of space or tab characters”, andK
of course means “at this point match a K”.For your added question could do something like this:
(The
\zs
says “consider the match to have started at this point” so only the “K” will be replaced.)But I would only do that if I had to do the substitution in a single pass. Otherwise I’d just run three
s///
s:Given command history, that’s much less typing, and is also very unlikely to require debugging, whereas that’s always a potentiality when specifying any computation.