regex - Ruby Regular Expression - prevent overlapping matches -
say have tag <tag> , want match groups of <tag>...<tag> in string. can use regular expression along lines of <tag>.*<tag>. matches <tag>foo<tag>, good, matches <tag>foo<tag>bar<tag>, behavior don't want. want <tag>foo<tag> matched, bar excluded, , tag on end start of next match. how do this?
the simplest solution use lazy quantifier ? forces .* match few characters possible (and not many possible, unadorned .* try match):
<tag>.*?<tag> a safer, more explicit solution use negative lookahead assertion:
<tag>(?:(?!<tag>).)*<tag> while in current case, there no difference in behavior, second 1 extendable handle open/close tags, making sure nested tags aren't incorrectly matched:
<tag>(?:(?!</?tag>).)*</tag> when applied <tag>foo<tag>bar</tag>baz</tag> match <tag>bar</tag>, , not <tag>foo<tag>bar</tag> solution lazy quantifier would.
Comments
Post a Comment