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";
}
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.
+ 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.
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
/(fred)+/ matches 1 or more 'fred',like fred fredfred fredfredfredfred
() can also catch the patten and put in \1 \2 \3 ... accordingly.
$_="yabba dabba doo";
if(/y(....) d\1){
print "It matched the same after y and d!\n";
}
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(/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 ......
$_="aa11bb";
if(/(.)\111/){
print "It matched!\n";
}
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";
}
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.
/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.
[^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
[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
[^\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!
[\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;
}
}
##########################
if(/fred/){
print;
}
}
##########################
2. Modify above program,also print the line with Fred in it.
#!/usr/bin/perl
while(<>){
if(/[Ff]red/){
print;
}
}
##########################
if(/[Ff]red/){
print;
}
}
##########################
3. Write a program,print only if the input line has a '.'
#!/usr/bin/perl
while(<>){
if(/\./){
print;
}
}
##########################
if(/\./){
print;
}
}
##########################
!/usr/bin/perl
while(<>){
if(/[A-Z][a-z]+/){
print;
}
}
##########################
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;
}
}
##########################
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;
}
}
if(/fred.*wilma|wilma.*fred/){
print;
}
}
##########################
OR
#!/usr/bin/perl
while(<>){ if(/fred/){ if(/wilma/){ print; } } }##########################