开发者

Regex: Lookaround && some syntax

开发者 https://www.devze.com 2023-01-14 09:49 出处:网络
I am studying about regular expression and struck with the lookaround concept and with few syntax. After doing googling, I thought it is a right forum to ask for help.

I am studying about regular expression and struck with the

  1. lookaround concept

    and

  2. with few syntax.

After doing googling, I thought it is a right forum to ask for help. Please help with this concept.

As I am not good with understanding the explanation. It will be great if I get plenty of different examples to understand.

For me the modifer /e and || are new in regex please help me in understa开发者_高级运维nding the real use. Below is my Perl Script.

$INPUT1="WHAT TO SAY";
$INPUT2="SAY HI";
$INPUT3="NOW SAY![BYE]";
$INPUT4="SAYO NARA![BYE]";

$INPUT1=~s/SAY/"XYZ"/e;   # /e What is this modifier is for

$INPUT2=~s/HI/"XYZ"/;

$INPUT3=~s/(?<=\[)(\w+)(?=])/ "123"|| $1 /e; #What is '||' is use for and what its name
$INPUT4=~s/BYE/"123"/e;

print "\n\nINPUT1 = $INPUT1 \n \n ";
print "\n\nINPUT2 = $INPUT2 \n \n ";
print "\n\nINPUT3 = $INPUT3 \n \n ";
print "\n\nINPUT4 = $INPUT4 \n \n ";


Have a read of perlrequick and perlretut.

The /e modifier of the s/// substitution operator treats the replacement as Perl code rather than as a string. For example:

$x = "5 10"
$x =~ s/(\d+) (\d+)/$1 + $2/e;
# $x is now 15

Instead of replacing $x with the string "$1 + $2", it evaluates the Perl code $1 + $2 - where $1 is 5 and $2 is 10 - and puts the result into $x.

The || is not a regex operator, it's a normal Perl operator. It is the logical-or operator: if the left-hand side is a true value (not 0 or ''), it returns the left side, otherwise it returns the right side. You can look up perl operators in perlop.


A standard substitution operator looks like this:

s/PATTERN/REPLACEMENT/

Where the PATTERN is matched, it is replaced with REPLACEMENT. REPLACEMENT is treated as a double-quoted string so that you can put variables in there and it will just work.

s/PATTERN/$var1/

You can use this to include pieces of the matched test in your replacement.

s/PA(TT)ERN/$1/

Sometimes, however, this isn't enough. Perhaps you want to process the text and run a subroutine to work out what the replacement is. Here's a really contrived example. Suppose you have text that contains floating point numbers and you want to replace them with integers. A first approach might look like this:

#!/usr/bin/perl

use strict;
use warnings;

$_ = '12.34 5.678';

s/(\d+\.\d+)/int($1)/g;

print "$_\n";

That doesn't work, of course. You end up with "int(12.34) int(5.678)". But that string is a piece of code which you want to run in order to get the correct answer. That's what the /e option does. It treats the replacement string as code, runs it and uses the output as the replacement.

Changing the line in the example above to

s/(\d+\.\d+)/int($1)/ge;

gives us the the required result.

Now that you understand /e I hope that you don't need an explanation of ||. It's just the standard or operator that you use all the time. In your example, it means "the replacement string is either '123' or the contents of $1'. Of course, that doesn't make much sense as '123' is always going to be true, so $1 will never be used. Perhaps you wanted it the other way round - $1 or '123'.

0

精彩评论

暂无评论...
验证码 换一张
取 消