如何编写正则表达式来重复捕获较大匹配中的组?
我对正则表达式感到头疼,所以希望有人可以帮助我。我正在做一些文件语法转换,并且文件中出现了这种情况:
OpenMarker
keyword some expression
keyword some expression
keyword some expression
keyword some expression
keyword some expression
CloseMarker
我想匹配标记内“关键字”的所有实例。标记区域是重复的,关键字可以出现在其他位置,但我不想匹配标记之外的区域。我似乎无法解决的是如何让正则表达式提取所有匹配项。我可以让一个人完成第一个或最后一个任务,但不能完成所有任务。我相信这应该是可能的,并且这与重复捕获组有关 - 有人可以向我展示光明吗?
我正在使用 grepWin,它似乎支持所有附加功能。
I'm getting a regex headache, so hopefully someone can help me here. I'm doing some file syntax conversion and I've got this situation in the files:
OpenMarker
keyword some expression
keyword some expression
keyword some expression
keyword some expression
keyword some expression
CloseMarker
I want to match all instances of "keyword" inside the markers. The marker areas are repeated and the keyword can appear in other places, but I don't want to match outside of the markers. What I don't seem to be able to work out is how to get a regex to pull out all the matches. I can get one to do the first or the last, but not to get all of them. I believe it should be possible and it's something to do with repeated capture groups -- can someone show me the light?
I'm using grepWin, which seems to support all the bells and whistles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用:
这将匹配
OpenMarker
和CloseMarker
中的关键字
(使用选项“点匹配换行符”)。You could use:
this will match the
keyword
insideOpenMarker
andCloseMarker
(using the option "dot matches newline").sed -n -e '/OpenMarker[[:space:]]*CloseMarker/p' /path/to/file | grep 关键字 应该可以工作。不确定 grep 是否可以单独做到这一点。
sed -n -e '/OpenMarker[[:space:]]*CloseMarker/p' /path/to/file | grep keyword
should work. Not sure ifgrep
alone could do this.只有少数正则表达式引擎支持重复组的单独捕获(例如.NET)。因此,最好的选择是分两步执行此操作:
首先匹配您感兴趣的部分:
OpenMarker(.*?)CloseMarker
(使用选项“点匹配换行符”)。然后重复应用另一个正则表达式来匹配:
keyword (.*)
(这次没有选项“点匹配换行符”)。There are only a few regex engines that support separate captures of a repeated group (.NET for example). So your best bet is to do this in two steps:
First match the section you're interested in:
OpenMarker(.*?)CloseMarker
(using the option "dot matches newline").Then apply another regex to the match repeatedly:
keyword (.*)
(this time without the option "dot matches newline").