Ask and Learn
Ask and Learn

Reputation: 8959

Confused by Perl lookahead and lookbehind

I am confused by Perl lookahead (?=regex) and lookbehind (?<=regex) and need some help to understand it.

Does lookahead mean look to the right of (?=regex)

Does lookbehind mean look to the left of (?<=regex)

I also noticed that lookbehind (?<=regex) only works with fixed width regex, I use a simple example to ask next question.

For example, give following lines of code, I want to match numbers but only if it is not in a comment line. So it should match 2, not 1

 #Comment 1
 my $number = 2

I tried following

/(?<!^#)\d/ 
match a number if the line does not start with #

That did not work, is that because it is not a fixed width lookbehind regex ?

Thanks

Upvotes: 1

Views: 621

Answers (1)

Miller
Miller

Reputation: 35198

Correct, lookahead and lookbehinds search from the place they are inside the regex. So your example /(?<!^#)\d/ will match any digit as long as it's not immediately following a # at the start of a string.

For example:

my $string = "123 #456 #789"
while ($string =~ /(?<!#)(\d+)/g) {
     print $1;
}

The above will print 1235689. Only the 4 and 7 will be skipped because they are immediately preceded by a #

Update

To talk about your specific regex:

use strict;
use warnings;

my $string = "#123 #456 #789";
while ($string =~ /(?<!^#)(\d+)/g) {
     print $1;
}

The above will print 23456789, because only the number 1 is preceded by a # that is at the start of a string.

Upvotes: 6

Related Questions