Perl Lookaround Statements

im confused about using these statements in perl?

an example of this:

(?=pattern) 

or a positive outlook. So here are my questions:

  • How are they useful? what instances are they used?
  • And related to question 1, why do I want to look towards the regex pattern? Isn't that a job? looking forward and then perform pattern matching again.

I need a very clear example, if possible. Thanks

+4
source share
5 answers

In uppercase, between commas, you can 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 havenโ€™t used images, this is what you got,

 (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 look not allow repeating, it does not work without it.


Another common use is the part of the 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 not a digit. 

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


It is also useful in more esoteric patterns.

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

can be written as

  /^(?=.*?a)(?=.*?b).*?c/s 
+4
source

lookahead allows lookahead to validate a template without actually matching .

When you execute a(?=b) , you must match a if followed by b . Note : it does not match b .


So,

1> You can extract hello (without # ) from #hello# with

 (?<=#)hello(?=#) 

2> You can check passwords with requirements such as a password must have 2 digits, 2 letters or more with any other character

 ^(?=(.*\d){2})(?=(.*[az]){2}).*$ 

Try to do it without looking, you recognize the importance

+3
source

I found the views especially useful for checking several conditions. For example, consider a regular expression that checks that a password has at least one lowercase, one uppercase, one numeric, and one character characters and is at least 8 characters long:

 ^(?=.*[az])(?=.*[AZ])(?=.*[0-9])(?=.*[^a-zA-Z0-9]).{8,}$ 

Try creating a regex to do the same without pending statements! It is possible, but it is very cumbersome.

Meanwhile, I found lookbehinds to be especially useful for checking the boundary conditions & mdash, that is, for example, matching a string from 0, unless it is preceded by a different number, for example 1000067 .

These are my experiences, but, of course, there are many practical applications, and everyone who uses the tool can vary from person to person.

+1
source

There are many reasons to use return paths, for example.

  • restriction of a substring that is considered consistent: s/(?<=[0-9])+(?=[0-9])/-/ instead of s/([0-9])+([0-9])/$1-$2/ .
  • and along with various conditions: /(?=\p{Uppercase}\p{Lowercase})\p{InBasicLatin}{2,}/ .
0
source

Lookaround statements are useful when you need a template to help you find a match, but you don't want the template to be part of the captured one.

Here is a simple look-up scenario:

Say I have

 my $text = '98 degrees, 99 Red Balloons, 101 Dalmatians' 

and I want to change the number of red balls from the previous value to 9001 , so I use

 $text =~ s/\d+(?=Red Balloons)/9001/; 
0
source

Source: https://habr.com/ru/post/1484768/


All Articles