Perl Learning - 12 (Perl RE)

Perl Learning - 12 (Perl RE),第1张

概述Another good feature of Perl is RE, PerlRE. In Perl, RE usually means patten, a match (or unmatch) of some characters template. The patten can divide any characters into two parts: match and unmatch. Another good feature of Perl is RE,PerlRE. In Perl,RE usually means patten,a match (or unmatch) of some characters template. The patten can divIDe any characters into two parts: match and unmatch. All RE does is to determine match or unmatch to a certain characters.   $_="yabba dabba doo";
if(/abba/){
 print "It matched!\n";
}   Expression /abba/ trIEs to find 'abba' from $_,if found then true otherwise false. All skills used in " " can be used in RE patten.   There are some magic characters do magic match in RE.   * is 0 or more times of character before it.
+ is 1 or more times of character before it.
? is o or 1 times of character before it.   a* means 0 or more 'a',like a aa aaaaaa or nothing(0 times).
a+ means 1 or more 'a',like a aa aaaaa.
a? means 0 or 1 'a',that's a or thing.   () can make group in patten.   /fred+/ matches 'fre' with 1 or more 'd',like fred fredd fredddddd
/(fred)+/ matches 1 or more 'fred',like fred fredfred fredfredfredfred   () can also catch the patten and put in \1 \2 \3 ... accordingly.   $_="abba";
if(/(.)\1){ # matched bb
 print "It matched same character next to itself!\n";
}   $_="yabba dabba doo";
if(/y(....) d\1){
 print "It matched the same after y and d!\n";
}   $_="yabba abba doo";
if(/y(.)(.)\2\1/){
 print "It matched the same after y and d!\n";
}   If there are () in (),we count the left ( refer to the number \1 \2 \3 ......   (  # The first one,\1
 (.) # The second,\2
 (.) # The third,\3
 \3
 \2
)   $_="aa11bb";
if(/(.)\111/){
 print "It matched!\n";
}   Perl trIEs to explain \111,which fails. In Perl 5.10,we can use \g{1} to catch \1   $_="aa11bb";
if(/(.)\g{1}11/){
 print "It matched!\n";
}   /fred|barney|betty/ matches fred OR barney OR betty,if one of them exists,then matches.   /fred( |\t)+barney/ matches fred and barney with one or more space to tab in them.   /fred (and|or) barney/ matches 'fred and barney' OR 'fred or barney'.
/fred and barney|fred or barney/ matches the same as above.   [] matches ONE of the characters in it.   [abcwxyz] matches anyone of the 7 characters 'abcwxyz'
[^ab] matches any character but not 'a' or 'b'
[abcwxyz] is same as [a-cw-z],[a-zA-Z] matches all 52 English characters.
[0-9] matches all numberic characters. [0-9] cab be written as \d
[A-Za-z0-9_] can be written as \w
[\f\t\n\r ] can be written as \s   [^\d] can be written as \D
[^\w] can be written as \W
[^\s] can be written as \S   . matches any character but not "\n"
[\d\D] matches any numberic or non-numberic,that's any character including "\n"
[^\d\D] matches nothing at all!   Exercises:   1. Write a program,read data from input,print the line when meets 'fred'.   #!/usr/bin/perl while(<>){
 if(/fred/){
  print;
 }
}
##########################   2. Modify above program,also print the line with Fred in it.   #!/usr/bin/perl while(<>){
 if(/[Ff]red/){
  print;
 }
}
##########################   3. Write a program,print only if the input line has a '.'   #!/usr/bin/perl while(<>){
 if(/\./){
  print;
 }
}
##########################   4. Write a program,print the lines that has words with first char upper case.   !/usr/bin/perl while(<>){
 if(/[A-Z][a-z]+/){
  print;
 }
}
##########################   5. Write a program,print the line with a non-space character stands besIDes itself.   #!/usr/bin/perl while(<>){
 if(/(\S)\g{1}/){
  print;
 }
}
##########################   6. Write a program,print the lines with both wilma and fred.   #!/usr/bin/perl while(<>){
 if(/fred.*wilma|wilma.*fred/){
  print;
 }
} ##########################   OR   #!/usr/bin/perl while(<>){ if(/fred/){  if(/wilma/){   print;   }  } }########################## 总结

以上是内存溢出为你收集整理的Perl Learning - 12 (Perl RE)全部内容,希望文章能够帮你解决Perl Learning - 12 (Perl RE)所遇到的程序开发问题。

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

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

原文地址: https://outofmemory.cn/langs/1293058.html

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

发表评论

登录后才能评论

评论列表(0条)

保存