正则表达式匹配每个文件和每行的多个匹配项
第一篇文章,就到这里吧。 我正在编写一个在文件树上进行智能搜索和替换的脚本。 本质上,该脚本将每个文件的内容获取到缓冲区字符串中,并与预定义的模式进行匹配,在本例中,模式为 /^[^\r\n]*(vendor)[^\r\ n]*$/im
。 该模式应该找到任何不区分大小写的“供应商”形式,并为我提供匹配的整行。 我需要查看整行才能确定是否替换该事件。 此模式对于给定文件中出现的任意数量的单独行都表现良好。
但是,如果给定行上出现多个匹配项,则该模式将仅匹配其中一个匹配项,并跳过其他匹配项。 我试图确定是否需要第二个模式来对匹配的行进行额外的搜索,或者我是否可以使用执行上述所有操作的单个模式来实现此目的。
只是我希望生产的一个样本。 单行多次出现匹配的示例:
热情地进行革命 支持网络的供应商潜力 供应商安装了基础电子零售商。
我想输出与此类似的内容(同样,输出不是这里的问题,而是我是否可以将其全部与一种模式匹配):
行中多次出现:|| 热心革新 支持网络的供应商潜力 供应商安装了基地电子零售商。 ||
更换? (y,n,q) || ...有能力的供应商潜力... ||
更换? (y,n,q) || ...领带供应商安装... ||
如果我不清楚我的目标,请告诉我。 感谢您对此的任何帮助和回复!
First post, so here goes. I'm writing a script that does intelligent search and replace on a file tree. Essentially, the script gets each file's contents into a buffer string and performs a match with a pre-defined pattern, in this case the pattern is /^[^\r\n]*(vendor)[^\r\n]*$/im
. The pattern should find any case-insensitive forms of "vendor" and give me the entire line that was matched. I need to see the whole line in order to determine whether or not to replace the occurrence. This pattern performs well for any number of separate line occurences in a given file.
However, if there are multiple occurrences on a given line, the pattern will only match one of those occurrences, and skip the others. I'm trying to determine if I need to have a second pattern to do additional searching on the matched line, or if I can accomplish this purpose with a single pattern that does all of the above.
Just a sample of what I'm hoping to produce. An example of a single line, multi-occurrence match:
Enthusiastically revolutionize
web-enabled VENDOR potentialities
vendors installed base e-tailers.
I would like to output something similar to this (again, the ouput isn't the issue here, it's whether or not I can match it all with one pattern):
Multiple occurrences in line: ||
Enthusiastically revolutionize
web-enabled VENDOR potentialities
vendors installed base e-tailers. ||Replace? (y,n,q) || ...abled VENDOR poten... ||
Replace? (y,n,q) || ...ties vendors insta... ||
Please let me know if I am unclear on my objective. Thanks for any help and response to this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在使用
我建议添加 g (全局)运算符
If you are using
I would suggest adding the g (global) operator