Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What does positive look-ahead assertion doing here 2

Status
Not open for further replies.

varakal

IS-IT--Management
Mar 18, 2004
114
US
Code:
print "Palindrome is $1\n" 
   while ($str =~ m/(?=((.+).?(??{reverse($2)})))/g);

when a string like 'sdsd' is given, the above code will give all palindromes such as 'sds' and 'dsd'

If we remove the positive look ahead assertion as below:

Code:
print "Palindrome is $1\n" 
   while ($str =~ m/((.+).?(??{reverse($2)}))/g);

we get the result as only 'sds' which is the first palindrome. The while loop or 'g' moderator alone will not do the work.

I was wondering what exactly this look ahead assertion is doing in this particular case?

Thanks.
 
In your example, without the "plaa" the regex would match the first palindrome as you see but also it would consume all those characters such that they cannot be used in subsequent matches (as triggered by the /g).
With the plaa, no characters are consumed so the next match can start 1 character further on from where the last one started.

Does that help?


Trojan.
 
That helps. But then what is the need of /g moderator. I was under the impression that /g moderator will look for each and every character globally? I noticed that if we remove this /g, the while loop will infinitely print the first palindrome.

Though I got something similar to above regex, I got the exact solution from perlmonks.
 
Is it something like 'plaa' will give you from where to start next, and '/g' is actually doing the work?
 
Yes,
The /g makes it repeat the regex from the next char where the last match left off.
Without the plaa, there would only be 1 char left and therefore no more matches.
With the plaa, the /g repeats from the same place but 1 char further on (since it won't restart from exactly the same place because of perpetual looping).


Does that answer your question?

Trojan.
 
On the issue with endless looping of the while loop when you leave out the /g - that's because without the /g, each time you try the match, it's a standalone match, so it will always start at the beginning of the string (and therefore will always return true if the regexp matches at all). As TrojanWarBlade has said, with the /g modifier, it continues the attempted match from where it left off the last time, so it will eventually return false when it can't find any more matches.
 
Yes, that answered a lot of questions. Thanks guys.
 
You might like to bear in mind that use of this code would be questionable in anything long term.
It makes use of a regex feature that is considered experimental.

"(??{ code })"
WARNING: This extended regular expression feature is consid-
ered highly experimental, and may be changed or deleted with-
out notice.

Trojan.
 
I noticed it in perldoc. I tried to get the result in one regular expression. Is there any other way that we can get this in one regex?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top