我如何编写正则表达式来重复捕获更大匹配的组? [英] How can I write a regex to repeatedly capture group within a larger match?

查看:155
本文介绍了我如何编写正则表达式来重复捕获更大匹配的组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个正则表达式头痛,所以希望有人能帮助我。我正在做一些文件语法转换,并且在文件中出现了这种情况:

  OpenMarker 
关键字some表达式
关键字一些表达式
关键字一些表达式
关键字一些表达式
关键字一些表达式
CloseMarker

我想匹配标记内的所有关键字实例。标记区域重复出现,关键字可以出现在其他地方,但我不想在标记之外进行匹配。我似乎无法解决的是如何得到一个正则表达式来取消所有的比赛。我可以让一个人做第一个或最后一个,但不能得到所有的人。我相信它应该是可能的,这是重复捕获组的事情 - 有人能让我看到光吗?

我正在使用grepWin,它似乎支持所有您可以使用:

<$ p $

(??。CloseMarker)

<?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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆