Perl-正则

[oracle@jhoa 1]$ cat 5.pl 
my $string = "This string contains the number 25.11.";
if ($string =~ /-?(d+).?(d+)/){print "$1 is $1
"};
if ($string =~ /-?(d+).?(d+)/){print "$2 is $2
"};

[oracle@jhoa 1]$ perl 5.pl 
$1 is 25
$2 is 11

[oracle@jhoa 1]$ cat 5.pl 
my $string = "This string contains the number 25.11 test";
if ($string =~ /(d+).(d+)/){print "$1 is $1
"};

[oracle@jhoa 1]$ perl 5.pl 
$1 is 25

匹配模式用变量$&,包含不在括号中的

[oracle@jhoa 1]$ cat 5.pl 
my $string = "This string contains the number 25.11.";
#if ($string =~ /-?(d+).?(d+)/){print "$1 is $1
"};
#if ($string =~ /-?(d+).?(d+)/){print "$2 is $2
"};
if ($string =~ /-?(d+).?(d+)/){print "$& is $&
"};

[oracle@jhoa 1]$ perl 5.pl 
$& is 25.11


匹配处之前的部分用变量$`
[oracle@jhoa 1]$ cat 5.pl 
my $string = "This string contains the number 25.11.";
#if ($string =~ /-?(d+).?(d+)/){print "$1 is $1
"};
#if ($string =~ /-?(d+).?(d+)/){print "$2 is $2
"};
if ($string =~ /-?(d+).?(d+)/){print "$` is $`
"};

[oracle@jhoa 1]$ perl 5.pl 
$` is This string contains the number 



匹配处之后的部分用变量$'
[oracle@jhoa 1]$ cat 5.pl 
my $string = "This string contains the number 25.11 test";
#if ($string =~ /-?(d+).?(d+)/){print "$1 is $1
"};
#if ($string =~ /-?(d+).?(d+)/){print "$2 is $2
"};
if ($string =~ /-?(d+).?(d+)/){print "$' is $'
"};

[oracle@jhoa 1]$ perl 5.pl 
$' is  test

(?<c>pattern),其中c是一个字符,pattern是起作用的模式或子模式



1、(?:pattern)不存贮括号内的匹配内容(加问号取消存储)
括号内的子模式将存贮在内存中,此功能即取消存贮该括号内的匹配内容,如/(?:a|b|c)(d|e)f1/中的1表示已匹配的d或e,而不是a或b或c。

这个:就是<c>所代表的字符



如/(?:a|b|c)(d|e)f1/中的1表示已匹配的d或e,而不是a或b或c。

[oracle@jhoa 1]$ cat 7.pl 
my $a = 'adfa';
if ($a =~ /(a|b|c)(d|e)f1/){print "11111
"};
[oracle@jhoa 1]$ perl 7.pl 
11111

这里的1 就是等于a

[oracle@jhoa 1]$ perl 7.pl 
11111
[oracle@jhoa 1]$ cat 7.pl 
my $a = 'adfd';
if ($a =~ /(?:a|b|c)(d|e)f1/){print "11111
"};
[oracle@jhoa 1]$ perl 7.pl 
11111

这里的1 就是等于d

-------------------------------------------------------------------------------

/pattern/(?=string)/ 肯定的和否东的预见匹配。 ?=?!

匹配后面的string的模式,相反的,(?!string)匹配后面非string的模式,(只是看了下,不作为匹配模式)如:

[oracle@jhoa 1]$ cat 8.pl 
$string = "25abc";
     $string =~ /abc(?=[0-9])/;
     $matched = $&; # $&为已匹配的模式,为abc,不是abc8
print "$matched is $matched
";

[oracle@jhoa 1]$ perl 8.pl 
$matched is 


[oracle@jhoa 1]$ cat 8.pl 
$string = "25abc0";
     $string =~ /abc(?=[0-9])/;
     $matched = $&; # $&为已匹配的模式,为abc,不是abc8
print "$matched is $matched
";

[oracle@jhoa 1]$ perl 8.pl 
$matched is abc


--------------------------------------------------------
贪婪规则和懒惰规则
贪婪规则 :尽量匹配尽可能多的相同字符,如/ab+/在字符串abbc中匹配的将是abb,而不是ab。若表达式中出现两个重复符号,perl遵守贪婪规则。例:
$_="a xxx c xxxx c xxxx d";
/a.*c.*d/;
“.*”会和第二个c之前的所有字符符合。
 * + ? 都是贪婪的

在重复符号后加个问号,可以让它变得不贪心:
/a.*?c.*d/;
"a.*?c"会和最少的a,c之间字符匹配。


懒惰规则:模式匹配只要找到一个就停止。不再继续匹配



[oracle@jhoa 2]$ cat 1.pl 
my $a="a xxx c xxxx c xxxx d";
if ($a =~ /a.*c/){print "$& is $&
"};

[oracle@jhoa 2]$ perl 1.pl 
$& is a xxx c xxxx c
[oracle@jhoa 2]$


[oracle@jhoa 2]$ cat 1.pl       
my $a="a xxx c xxxx c xxxx d";
if ($a =~ /a.*?c/){print "$& is $&
"};

[oracle@jhoa 2]$ perl 1.pl 
$& is a xxx c

"a.*?c"会和最少的a,c之间字符匹配。

------------------------------------------------------------------------
[oracle@jhoa 2]$ cat 2.pl 
#$line="block1 first block2 second block3 third";
$line="block1 first block2";
$line=~/blockd(.*?)(?=blockd|$)/;print $1;

[oracle@jhoa 2]$ perl 2.pl 
 first [oracle@jhoa 2]$

[oracle@jhoa 2]$ cat 2.pl 
#$line="block1 first block2 second block3 third";
$line="block1 first block";
$line=~/blockd(.*?)(?=blockd|$)/;print $1;

[oracle@jhoa 2]$ perl 2.pl 
 first block[oracle@jhoa 2]$










原文地址:https://www.cnblogs.com/hzcya1995/p/13351952.html