Question

I want to find how the backtracking works inside the negative lookahead. like in ...

     ^(?!.*some My Words$).*$

how far the the regex engine will try the first .*. I am testing somewhere awesome My Words. Which I expect should be rejected, but not. So how the backtracking is working here and how this is happening?

Was it helpful?

Solution

The sample example would be rejected for the above regex.

.* eats everything and reaches to the end. i.e after last s.Then it backtracks 1 by 1 till it matches some My Words.

With .*? it would match till the first some My Words


.*? is better than .* but performance gain would be significant only for large strings

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top