How does a negative look with stars?

I am trying to understand why I am not getting the expected results from regex.

I already know what a negative look is (apparently not :-)) And also that the stars are zero or more times of repetitions.

Looking at this regex:

a(?![^3]) 

Regular expression visualization

This will match a followed by non-3 after it.

So, looking at this test line, the bold part corresponds to:

a 3333335

Ok

Also, if I change the regex to:

 a(?![^3]+) //notice "+" 

Regular expression visualization

It will still match:

a 3333335

This will match a followed by a non-3 (at least one)

Question

My problem is with * :

Change the regex to:

 a(?![^3]*) 

Regular expression visualization

It will not match

a3333335

But my question is: why?

According to the picture:

a should not: Either neither , nor <3>

But this happens: a not followed by anything And non3-'s should not be

So why is this not appropriate?

And to complicate my life:

Looking at this regex:

 a(?![^3]*7) 

This will match:

a 3333335

What's going on here?

+8
javascript regex
source share
4 answers

The problem is that the asterisk can generate an empty string ( "" ), and you can say that there is an empty string between each character and the next .

Given a regex:

 a(?![^3]*) 

and you ask with a33333 , you more or less say: reject if after a there are zero or more non-3 repetitions, but there is such a repetition: an empty string, so even without capturing one 3 , it will reject. The match looks like this:

 a (?![^3]*) "a" "" "33333" 

(quotes mark lines and are not characters here)

Thus, you can say that the negative look of the regular expression over the Klein star will always be rejected (you have to be careful, in the sentence I mean that the Klein star is combined over the "whole" regular expression, this does not mean that the negative look containing Klein star will always be rejected).

Your image also shows this:

enter image description here

He says that if you do not follow, it means that he cannot correspond to what is inside the field. The problem is that you do not need to take a single character to reach the end of the window.


This fails for a(?![^3]*7) : here you say: "* reject if you encounter zero or more non-3 followed by seven. Since the regular expression [^3]*7 not matches 3333335 , then the lookahead will not reject the match.

+6
source share

The problem is that * will repeat zero or more times. Zero non-3s (aka empty string). Nothing means end of line, it means literally nothing (empty line).

The reason the 7 example works is because lookahead will try to match as many non-3s as possible. In position after a this will be non-3s zero. After that, he will try to match exactly one 7 . But the next character is 3 , so lookahead won't let you match a .

+5
source share

To understand why:

 a(?![^3]*) 

not suitable, you can change your forecast:

 a(?!.*) 

which will also fail and cause your statement in header a not be accompanied by anything, including an empty string, will always return false, so your regular expression will always fail .

Second regex:

 a(?![^3]*7) 

which succeeds because there really is not 0 or more non-3 followed by 7 .

If you change your input, for example, a7 , then this will lead to a failure.

+3
source share

The problem is that * behaves the same as elsewhere.

[^ 3] * will match every line, because it matches "no more 3s, including none."

a (?! [^ 3] * 7) Must match "any" a "followed by not as much as 3 as possible, including" none "and" 7. "which is equivalent to" any "a, not followed 7 if they are not separated by "3".

0
source share

All Articles