Perl Learning - 12 (Perl RE)

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;   }  } }##########################

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...