Perl Learning - 14 (Catch var, (:?), (:<LABLE>), \g{LABLE}, {m,n}

Perl Learning - 14 (Catch var, (:?), (:<LABLE>), \g{LABLE}, {m,n},第1张

概述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 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 $1\n"; # print "the word was there"
}   $_="Hello there,neighbor";
if(/(\S+) (\S+),(\S+)/){
 print "words were $1 $2 $3\n";
}   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 $1\n";
}   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+a\b/){
  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+a\b)/){
  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+a\b)/){
  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+a\b)(?<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"; }}########################## 总结

以上是内存溢出为你收集整理的Perl Learning - 14 (Catch var, (:?), (:<LABLE>), \g{LABLE}, {m,n}全部内容,希望文章能够帮你解决Perl Learning - 14 (Catch var, (:?), (:<LABLE>), \g{LABLE}, {m,n}所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1293056.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存