Perl Learning - 12 (Perl RE)
2012-11-15 21:03
274 查看
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;
}
}
}
##########################
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;
}
}
}
##########################
相关文章推荐
- CodingNet - Learning - 12
- Learning Perl: 1.2. What Does "Perl" Stand For?
- Learning Perl: 4.3. Return Values
- Learning Perl: 10.6. Autoincrement and Autodecrement
- learning perl第5章练习题
- Learning Perl(Perl语言入门)学习笔记(1)
- Learning Perl, 5th Edition [ILLUSTRATED]
- 12 papers to understand QA system with Deep Learning
- Learning Perl 第六章习题第一题
- Perl 最佳实践(节选) --- 12
- Perl学习笔记 No.12
- Learning Perl ... (转载)
- Learning Perl: 1.3. How Can I Get Perl?
- Learning Perl: 4.7. Notes on Lexical (my) Variables
- Spring v3.0.2 Learning Note 12 - Integrate with Hiberate
- Perl Learning - 6 (subroutine, function, my)
- Perl Learning 1 Scalar Data
- Perl语言学习笔记 12 文件测试
- Learning Perl: 1.4. How Do I Make a Perl Program?
- Learning Perl: 4.8. The use strict Pragma