regex: Why this negative lookahead doesn't work? -
i have text this
real:: real :: b real c now want match real without :: followed, , in case, want match 3rd real. tried regex lookahead
real\s*(?!::) but matches
real :: b real c for \s* means 0 or more \s, why real :: b being matched?
update
thanks wiktor stribiżew. using regex101 debugging tool. can find backtrack makes thing complicated.
i came task similar can't solve
real (xx(yy)) :: real (zz(pp)):: b real (cc(rr)) c again, want match real (cc(rr)) without :: following.
real\s*\(.*?\)+(?!\s*::) this tried, failed. regex debug, due backtrack. how correctly?
you need put \s* lookahead:
real(?!\s*::) see regex demo
the real\s*(?!::) matches real because real matches real, \s* matches 0 or more whitespaces, lookahead fails match @ :: and engine backtracks, is, frees space matched \s* , tries re-match string. since \s* can match empty string, real before :: b gets matched.
see regex debugger scheme @ regex101 showing going on behind scenes:

Comments
Post a Comment