开发者

How can I write a regex to repeatedly capture group within a larger match?

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 t开发者_JAVA技巧o 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").


sed -n -e '/OpenMarker[[:space:]]*CloseMarker/p' /path/to/file | grep keyword should work. Not sure if grep alone could do this.


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").

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜