加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl-正则<2>

发布时间:2020-12-15 23:49:48 所属栏目:大数据 来源:网络整理
导读:[oracle@jhoa 1]$ cat 5.pl my $string = "This string contains the number 25.11.";if ($string =~ /-?(d+).?(d+)/){print "$1 is $1n"};if ($string =~ /-?(d+).?(d+)/){print "$2 is $2n"};[oracle@jhoa 1]$ perl 5.pl $1 is 25$2 is 11[oracl
[oracle@jhoa 1]$ cat 5.pl 
my $string = "This string contains the number 25.11.";
if ($string =~ /-?(d+).?(d+)/){print "$1 is $1n"};
if ($string =~ /-?(d+).?(d+)/){print "$2 is $2n"};

[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 $1n"};

[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 $1n"};
#if ($string =~ /-?(d+).?(d+)/){print "$2 is $2n"};
if ($string =~ /-?(d+).?(d+)/){print "$& is $&n"};

[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 $1n"};
#if ($string =~ /-?(d+).?(d+)/){print "$2 is $2n"};
if ($string =~ /-?(d+).?(d+)/){print "$` is $`n"};

[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 $1n"};
#if ($string =~ /-?(d+).?(d+)/){print "$2 is $2n"};
if ($string =~ /-?(d+).?(d+)/){print "$' is $'n"};

[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 "22222n"};
[oracle@jhoa 1]$ perl 7.pl 
22222

这里的1 就是等于a

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

这里的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 $matchedn";

[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 $matchedn";

[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 $&n"};

[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 $&n"};

[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]$









(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读