我如何编写正则表达式来重复捕获更大匹配的组? [英] How can I write a regex to repeatedly capture group within a larger match?
问题描述
OpenMarker
关键字some表达式
关键字一些表达式
关键字一些表达式
关键字一些表达式
关键字一些表达式
CloseMarker
我想匹配标记内的所有关键字实例。标记区域重复出现,关键字可以出现在其他地方,但我不想在标记之外进行匹配。我似乎无法解决的是如何得到一个正则表达式来取消所有的比赛。我可以让一个人做第一个或最后一个,但不能得到所有的人。我相信它应该是可能的,这是重复捕获组的事情 - 有人能让我看到光吗?
我正在使用grepWin,它似乎支持所有您可以使用:
<$ p $
<?c $ c>(?
这将匹配 OpenMarker
和关键字
> CloseMarker (使用dot matches newline选项)。
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.
You could use:
(?<=OpenMarker((?!CloseMarker).)*)keyword(?=.*CloseMarker)
this will match the keyword
inside OpenMarker
and CloseMarker
(using the option "dot matches newline").
这篇关于我如何编写正则表达式来重复捕获更大匹配的组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!