Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lookaround assertions in Perl

Tags:

regex

perl

im confused what is the use of these lookaround assertions in perl?

example this one:

(?=pattern)

or the positive lookahead. So here's my questions:

  1. How are these useful? what sort of instances they are used?
  2. And related to question 1, why would i want to look ahead of the regex pattern? isnt it more work? looking ahead and then executing the pattern matching again.

I need a very clear example if possible. Thanks

like image 931
Belmark Caday Avatar asked Feb 16 '23 08:02

Belmark Caday


1 Answers

To uppercase what's in between commas, you could use:

(my $x = 'a,b,c,d,e') =~ s/(?<=,)([^,]*)(?=,)/ uc($1) /eg;   # a,B,C,D,e

                  a,b,c,d,e
Pass 1 matches      -
Pass 2 matches        -
Pass 3 matches          -

If you didn't use lookarounds, this is what you'd get,

(my $x = 'a,b,c,d,e') =~ s/,([^,]*),/ ','.uc($1).',' /eg;   # a,B,c,D,e

                  a,b,c,d,e
Pass 1 matches     ---
Pass 2 matches         ---

Not only does the lookahead avoid repetition, it doesn't work without it!


Another somewhat common use is as part of a string equivalent to [^CHAR].

foo(?:(?!foo|bar).)*bar  # foo..bar, with no nested foo or bar

You can use it to narrow down character classes.

\w(?<!\d)     # A word char that's not a digit.

Although this can now be done using (?[ ... ]).


It's also useful in more esoteric patterns.

 /a/ && /b/ && /c/

can be written as

 /^(?=.*?a)(?=.*?b).*?c/s
like image 75
ikegami Avatar answered Feb 23 '23 06:02

ikegami