加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl Learning - 12 (Perl RE)

发布时间:2020-12-15 21:02:23 所属栏目:大数据 来源:网络整理
导读: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 unmat
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(....) d1){
?print "It matched the same after y and d!n";
}
?
$_="yabba abba doo";
if(/y(.)(.)21/){
?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
[ftnr ] 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"
[dD] matches any numberic or non-numberic,that's any character including "n"
[^dD] 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;???}??}?}##########################

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读