Regular Expression - explain difference between look-ahead and look-behind

Hi RegExperts!

I have a couple of questions that have been troubling me and I'm having a heck of a time finding useful (human)readable information on it. Let's say that Tom Christiansen isn't exactly light reading in his perlre and perlretut!

What's the difference, functionally, between a look-ahead and a look-behind?

IE: how is this: m/(?!foo)bar/ different from m/(?<foo)bar/

or this: m/(?!foo)bar/ different from m/(?<!foo)bar/

I'd prefer a written answer rather than a link to some tutorial online, unless it's a really solid and well-written tutorial.

LVL 14
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
I know you said you didn't want a link, but I really do think this is a very good explanation of lookahead and lookbehind:
käµfm³d 👽Commented:
m/(?!foo)bar/   ##  matches any string that contains the string "bar". The negative lookahead here is not very effective. A better use of it would be  m/bar(?!foo)/   which matches the string "bar" that is NOT immediately FOLLOWED by the string "foo"

m/(?<foo)bar/  ##  matches a string "bar" that IS immediately PRECEDED by the string "foo".

m/(?!foo)bar/  ##  matches any string that contains the string "bar". The negative lookahead here is not very effective (and I believe meaningless) as you are asserting that a string not match foo, but then should match the string "bar". Given this pattern, even "foobar" would be a match. A positive/negative lookahead are better served by going after the string you want (e.g.  m/bar(?!foo)/ )

m/(?<!foo)bar/  ##  matches a string "bar" that IS NOT immediately PRECEDED by the string "foo".

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
The correct syntax for look behind is

    positive:  (?<=)
    negative:  (?<!)

My post is actually incorrect as well  :)  Please make the appropriate adjustments where required.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

käµfm³d 👽Commented:
>>    Please make the appropriate adjustments where required.

Hmm.. that might be a bit confusing. The only change that should be made is:

    m/(?<foo)bar/  ##  matches a string "bar" that IS immediately PRECEDED by the string "foo".


   m/(?<=foo)bar/  ##  matches a string "bar" that IS immediately PRECEDED by the string "foo".
tomaugerdotcomAuthor Commented:
Thanks very much for the detailed an thoroughly understandable explanation. I actually had to laugh to myself when I realized what I had written (after you explained it).

I think the real insight here is that they are zero-width, which is why the positive lookahead doesn't work very well (or at all, in fact) in front of another word or token. If it matches the lookahead assertion, then chances are it won't match the word or token your looking for!

@ Matthew, thanks for the link. I awarded the points to kaufmed because he addressed my example specifically.

Thanks to both of you for taking your time!
käµfm³d 👽Commented:
NP. Glad to help.

And I did forget to mention that lookaheads/lookbehinds are zero-width. I wasn't sure how detailed you wanted to go, but I see you found that detail anyways.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.