开发者

regex: find match only if inside start/stop section

开发者 https://www.devze.com 2023-03-09 05:38 出处:网络
I need tofind MATCH only if it is between START and STOP: blah START hello MATCH bye STOP blah blah START hello MATCH STOP blah

I need tofind MATCH only if it is between START and STOP:

  1. blah START hello MATCH bye STOP blah

  2. blah START hello MATCH STOP blah

  3. blah MATCH blah START hello MATCH STOP blah

e.g.: on line 3, only the 2nd MATCH should be found.

Please no开发者_开发知识库te that START, STOP and MATCH are not literals. They are expressed as parts regex of the regex.

Of course, a simplified but non elegant solution would consist in extracting the part between START and STOP and then looking for MATCH in there.

TIA,


@"START.*?(MATCH).*?STOP"

Just capture whatever "MATCH" is. You may have additional criteria for the things that can occur between START - MATCH - STOP (as opposed to just "."), if so, use a character class.


Try this:

/\bSTART\b.*?\bMATCH\b.*?\bSTOP\b/

Of if you're trying to capture it and are not speaking of these actual words:

/START.*?(MATCH).*?STOP/
0

精彩评论

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

关注公众号