regex - Regular expression doesn't match if a character participated in a previous match -
i have regex:
(?:\s)\++(?:\s)
which supposed catch pluses in query string this:
?busca=tenis+nike+categoria:"tenis+e+squash"&pagina=4&operador=or
it should have been 4 matches, there 3:
s+n
e+c
s+e
it missing last one:
e+s
and seems happen because "e" character has participated in previous match (s+e), because "e" character right in middle of 2 pluses (teni s+e+s quash).
if test regex following input, matches last "+":
?busca=tenis+nike+categoria:"tenis_e+squash"&pagina=4&operador=or
(changed "s+e" "s_e" in order not cause "e" character participate in match).
would please shed light on that?
thanks in advance!
you correct: fourth match doesn't happen because surrounding character has participated in previous match. solution use lookaround (if regex implementation supports - javascript doesn't support lookbehind, example).
try
(?<!\s)\++(?!\s)
this matches 1 or more +
unless surrounded whitespace. works if plus @ start or end of string.
explanation:
(?<!\s) # assert there no space before current position # (but don't make character part of match itself) \++ # match 1 or more pluses (?!\s) # assert there no space after current position
if regex implementation doesn't support lookbehind, use
\s\++(?!\s)
that way, match contain character before plus, not after it, , therefore there no overlapping matches (thanks gumbo!). fail match plus @ start of string, though (because \s
need match character). not problem.
Comments
Post a Comment