Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

regex - Understanding positive and negative lookaheads

I'm trying to understand how negative lookaheads work on simple examples. For instance, consider the following regex:

a(?!b)c

I thought the negative lookahead matches a position. So, in that case the regex matches any string that contains strictly 3 characters and is not abc.

But it's not true, as can be seen in this demo. Why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Lookaheads do not consume any characters. It just checks if the lookahead can be matched or not:

a(?!b)c

So here after matching a it just checks if it is followed not by b but does not consume that not character (which is c) and is followed by c.

How a(?!b)c matches ac

ac
|
a

ac
 |
(?!b) #checks but does not consume. Pointer remains at c

ac
 |
 c

Positive lookahead

The positive lookahead is similar in that it tries to match the pattern in the lookahead. If it can be matched, then the regex engine proceeds with matching the rest of the pattern. If it cannot, the match is discarded.

E.g.

abc(?=123)d+ matching abc123

abc123
|
a

abc123
 |
 b

abc123
  c

abc123 #Tries to match 123; since is successful, the pointer remains at c
    |
 (?=123)

abc123 # Match is success. Further matching of patterns (if any) would proceed from this position
  |

abc123
   |
  d

abc123
    |
   d

abc123 #Reaches the end of input. The pattern is matched completely. Returns a successfull match by the regex engine
     |
    d

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...