Welcome to WebmasterWorld Guest from 54.146.201.80

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Regex

     
10:09 pm on Nov 19, 2008 (gmt 0)

New User

5+ Year Member

joined:June 25, 2008
posts: 24
votes: 0


I am really struggling to come to terms with look-ahead and look-behind concepts in Regular Expressions.

I am new to Perl and can only start to get an understanding with maybe an example that explains how these concepts work.

Can anyone help me with this?

6:13 pm on Nov 21, 2008 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8135
votes: 92


The lookahead is a 'zero width' expression so it allows you to see if you have a match, without having to capture the characters you're matching. So for example

a[^b] - matches "a" and whatever character follows an "a" as long as it's not a "b"

a(?!b) - matches "a" and only "a" provided that it is not followed by a "b" but it does not match the character after the "a".

So for example in "example" the first expression would capture "am" and the second would capture "a".

I'm trying to think of a case where you can't do it any other way, but I'm not coming up with one. You'll see, sometimes, that a negated character class won't get the job done though.

Example anyone?

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members