I have a hard time to understand the concepts of "lookahead" and "lookbehind". For example, there is a string "aaaaaxbbbbb". If we look at "x", does lookahead mean looking "x" towards "bbbbb" or "aaaaa"? I mean the direction.
-
3http://www.regular-expressions.info/lookaround.html – CaffGeek Jul 23 '12 at 21:12
-
I still don't understand it, I would like use example to demonstrate it. – Jul 23 '12 at 22:41
1 Answers
If the regex is x(?=insert_regex_here)
that is a (positive) look*ahead*, which looks ahead, or forwards, in other words towards "bbbb".
It means "find an x that is followed by insert_regex_here
".
If the regex is (?<=insert_regex_here)x
that is a (positive) look*behind*, which looks behind, or backwards, in other words towards "aaaa". It means "find an x that is preceded by insert_regex_here
".
You can also have negative lookahead x(?!insert_regex_here)
meaning "x
not followed by insert_regex_here
", and negative lookbehind (?<!insert_regex_here)x
, meaning "x not preceded by insert_regex_here
".
(The above (?=
and (?<!
etc are Perl regex syntax - the syntax might be slightly different depending on your flavour of regex).
I recommend you read the link that Chad gave in the comments. It has examples.

- 55,977
- 11
- 154
- 194
-
Erm. Those examples you gave were negative/positive lookbehind, and the first example lost a `<` :P Positive/negative lookahead would be without that first `<`. (also, the presence of all those angle brackets gets especially confusing :P) – Steve Wang Jul 23 '12 at 23:52
-
Oops sorry, I meant `(?=<` not `(?=<`. I'm going to remove those `<>` around, they *are* confusing :P – mathematical.coffee Jul 23 '12 at 23:55
-
9This syntax is a bit of a strange beast : the name obviously gives you the direction where the engine looks. But the syntax feels like duplication of information : when you want to look behind, you already write the zero-width match **behind** what you want to match, why is the engine also requiring a different operator ? (It is an actual question, in a comment ; ) – Ad N Feb 20 '14 at 17:19
-
4@AdN The problem is that look behinds must be fixed length, while lookahead can be variable length (or at least, most implementations do so) If you don't distinguish them syntactically you end up with incorrect results. – Bakuriu Aug 01 '16 at 16:31