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:

enter image description here


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -