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

Perl Learning - 14 (Catch var, (:?), (:<LABLE>

发布时间:2020-12-15 21:02:19 所属栏目:大数据 来源:网络整理
导读:Variable can be inserted in /patten/,similar to "$variable" ? #!/usr/bin/perl my $what="fred"; while(){ ?if(/^($what)/){ ?print "We saw $what in beginning of $_"; ?} } ? () can ctach the characters in it as varables,called $1,$2,$3 .... Th
Variable can be inserted in /patten/,similar to "$variable"
?
#!/usr/bin/perl
my $what="fred";
while(<>){
?if(/^($what)/){
?print "We saw $what in beginning of $_";
?}
}
?
() can ctach the characters in it as varables,called $1,$2,$3 ....

The first () gets $1,second gets $2,.... caculate by the left (
?
$_="Hello there,neighbor";
if(/s(w+),/){
?print "the word was $1n";?# print "the word was there"
}
?
$_="Hello there,neighbor";
if(/(S+) (S+),(S+)/){
?print "words were $1 $2 $3n";
}
?
The life sycle of a caught variable go until next successful match,which overide it.
If we only want () make group but not catch variable,we can use non-catch ().
?
$_="a saurus steak for dinner";
if(/(?:bronto)?saurus (steak|burger)/){
?print "Fred wants a $1n";
}
?
The ?: behind ( makes that () not catch variable.
?
We can name the caught variable instead of $1 $2 .... the numberic style.
?
my $names="Fred or Barney";
if($names=~/(?<name1>w+) (?:and|or) (?<name2>w+)/){
?print "I saw $+{name1} and $+{name2}n";
}
?
(?<LABLE>PATTEN) LABLE can be any name we like,all the names are stored in hash %+
?
Using the named variables we can insert more () not worrying the $number,g{1} can also named as g{lable}
?
my $names="Fred Flintstone and Wilma Flintstone";
if($names=~/(?<last_name>w+) and w+ g{last_name}/){
?print "I saw $+{last_name}n";?# Flintstone
}
?
if("Hello there,neighbor"=~/s(w+),/){
?print "That actually matched '$&'.n";
}
?
if("Hello there,/){
?print "That was ($`) ($&) ($').n";
}
?
$& is what /patten/ matches,$` is parts before $&,$' is parts after $&.
?
/a{m,n}/ matches 'a' appears m to n times
/a{m,}/ matches 'a' appears at least m times
/a{m}/ matches 'a' appears exactly m times
?
RE has its connect order like this:
?
Patten??Examples
()???(...)?(?:...)?(?<LABLE>...)
* + ? {}?a*?a+?a??a{m,n}
^ $ ??abc?^a ?a$
|???a|b|c
element??a ?[abc]?d ?1
?
Exersices:
?
1. Write a program,make it match 'match' and print the parts before and after it.
?
#!/usr/bin/perl
while(<>){
?if(/match/){
??print "That was |$`<$&>$'|n";
?}
}
##########################
?
2. Write a program,make it match words(made of w) end with 'a'.
?
#!/usr/bin/perl
while(<>){
?chomp;
?if(/w+ab/){
??print "That was |$`<$&>$'|n";
?}
}
##########################
?
3. Modify last program,catch the words end with 'a' in $1. And print it like: $1 contains 'Wilma'.
?
#!/usr/bin/perl
while(<>){
?chomp;
?if(/(w+ab)/){
??print "$1 contains '$1'n";
?}
}
##########################
?
4. Modify last program,catch words into named variable. And print it like: 'word' contains 'Wilma'.
?
#!/usr/bin/perl
while(<>){
?chomp;
?$lable="word";
?if(/(?<$lable>w+ab)/){
??print "'$lable' contains '$+{$lable}'n";
?}
}
##########################
?
5. Modify last program,? print the word ended with 'a' and the following 5 characters(if there are).
?
#!/usr/bin/perl
while(<>){
?chomp;
?if(/(?<word>w+ab)(?<more>.{0,5})/){
??print "$+{word}|$+{more}n";
?}
}
##########################
?
6. Write a new program,print all lines ended with sapce character(s),print more character(s) to make it human readable.
?
#!/usr/bin/perl
while(<>){?chomp;?if(/s+$/){??print "$_|n";?}}##########################

(编辑:李大同)

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

    推荐文章
      热点阅读