码迷,mamicode.com
首页 > 其他好文 > 详细

Perl正则表达式--练习

时间:2015-04-29 17:29:59      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:perl   正则表达式   练习   

练习题目:

1. 打印所有含有模式Street的行。

2. 打印名字与B或b相匹配的行。

3. 打印与Ker匹配的行。

4. 打印区号为408的电话号码。

5. 打印Gortz的名字和地址。

6. 以大写形式打印Ephram的名字。

7. 打印不含4的行。

8. 将William的名字改为Siegfield。

9. 打印Tommy Savage的生日。

10. 打印收入超过$40,000的名字。

11. 打印在6月出生的人的名字。

12. 打印Massachusetts的邮件编码。  


练习文件内容:

# cat sample.txt
Tommy Savage:408–724–0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
Lesle Kerstin:408–456–1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach:408–253–3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy:293–259–5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop:245–836–8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf:846–836–2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder:397–857–2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
James Ikeda:834–938–8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz:327–832–5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz:385–573–8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500


1. 打印所有含有模式Street的行

while (<DATA>) {
    ($name, $phone, $address, $birth, $salary) = split(":", $_);
    print if /Street/;
}

__DATA__
Tommy Savage:408–724–0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
Lesle Kerstin:408–456–1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach:408–253–3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy:293–259–5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop:245–836–8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf:846–836–2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder:397–857–2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
James Ikeda:834–938–8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz:327–832–5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz:385–573–8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

运行结果为:

技术分享


2. 打印名字与B或b相匹配的行

while (<DATA>) {
    ($name, $phone, $address, $birth, $salary) = split(":", $_);
    print if $name =~ /[Bb]/;
}

__DATA__
Tommy Savage:408–724–0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
Lesle Kerstin:408–456–1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach:408–253–3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy:293–259–5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop:245–836–8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf:846–836–2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder:397–857–2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
James Ikeda:834–938–8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz:327–832–5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz:385–573–8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

运行结果为:

技术分享


3. 打印与Ker匹配的行

while (<DATA>) {
    ($name, $phone, $address, $birth, $salary) = split(":", $_);
    print if /Ker/;
}

__DATA__
Tommy Savage:408–724–0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
Lesle Kerstin:408–456–1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach:408–253–3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy:293–259–5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop:245–836–8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf:846–836–2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder:397–857–2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
James Ikeda:834–938–8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz:327–832–5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz:385–573–8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

运行结果为:

技术分享


4. 打印区号为408的电话号码

while (<DATA>) {
    ($name, $phone, $address, $birth, $salary) = split(":", $_);
    print if $phone =~ /408/;
}

__DATA__
Tommy Savage:408–724–0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
Lesle Kerstin:408–456–1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach:408–253–3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy:293–259–5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop:245–836–8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf:846–836–2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder:397–857–2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
James Ikeda:834–938–8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz:327–832–5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz:385–573–8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

运行结果为:

技术分享


5. 打印Gortz 的名字和地址

while (<DATA>) {
    ($name, $phone, $address, $birth, $salary) = split(":", $_);
    print "$name -- $address\n" if $name =~ /Gortz/;
}

__DATA__
Tommy Savage:408–724–0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
Lesle Kerstin:408–456–1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach:408–253–3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy:293–259–5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop:245–836–8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf:846–836–2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder:397–857–2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
James Ikeda:834–938–8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz:327–832–5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz:385–573–8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

运行结果为:

技术分享


6. 以大写形式打印Ephram 的名字

while (<DATA>) {
    ($name, $phone, $address, $birth, $salary) = split(":", $_);
    print "\U$name\E" if $name =~ /Ephram/;
}

__DATA__
Tommy Savage:408–724–0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
Lesle Kerstin:408–456–1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach:408–253–3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy:293–259–5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop:245–836–8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf:846–836–2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder:397–857–2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
James Ikeda:834–938–8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz:327–832–5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz:385–573–8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

运行结果为:

技术分享


7. 打印不含4 的行

while (<DATA>) {
    ($name, $phone, $address, $birth, $salary) = split(":", $_);
    print unless ($. == 4);
}

__DATA__
Tommy Savage:408–724–0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
Lesle Kerstin:408–456–1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach:408–253–3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy:293–259–5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop:245–836–8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf:846–836–2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder:397–857–2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
James Ikeda:834–938–8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz:327–832–5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz:385–573–8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

运行结果为:

技术分享


8. 将William 的名字改为Siegfield

while (<DATA>) {
    ($name, $phone, $address, $birth, $salary) = split(":", $_);
    print if s/William/Siegfield/;
}

__DATA__
Tommy Savage:408–724–0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
Lesle Kerstin:408–456–1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach:408–253–3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy:293–259–5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop:245–836–8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf:846–836–2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder:397–857–2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
James Ikeda:834–938–8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz:327–832–5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz:385–573–8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

运行结果为:

技术分享


9. 打印Tommy Savage 的生日

while (<DATA>) {
    ($name, $phone, $address, $birth, $salary) = split(":", $_);
    print "$name birth is: $birth\n" if $name =~ /Tommy Savage/;
}

__DATA__
Tommy Savage:408–724–0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
Lesle Kerstin:408–456–1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach:408–253–3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy:293–259–5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop:245–836–8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf:846–836–2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder:397–857–2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
James Ikeda:834–938–8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz:327–832–5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz:385–573–8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

运行结果为:

技术分享


10. 打印收入超过$40,000的名字

while (<DATA>) {
    ($name, $phone, $address, $birth, $salary) = split(":", $_);
    print "$. -- $name\n" if ($salary > 40000);
}

__DATA__
Tommy Savage:408–724–0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
Lesle Kerstin:408–456–1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach:408–253–3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy:293–259–5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop:245–836–8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf:846–836–2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder:397–857–2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
James Ikeda:834–938–8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz:327–832–5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz:385–573–8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

运行结果为:

技术分享


11. 打印在6月份出生的人的名字

while (<DATA>) {
    ($name, $phone, $address, $birth, $salary) = split(":", $_);
    ($birth_month, $birth_day, $birth_year) = split("/", $birth);
    print $name if $birth_month =~ /6/;
}

__DATA__
Tommy Savage:408–724–0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
Lesle Kerstin:408–456–1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach:408–253–3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy:293–259–5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop:245–836–8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf:846–836–2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder:397–857–2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
James Ikeda:834–938–8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz:327–832–5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz:385–573–8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

运行结果为:

技术分享


12. 打印Massachusetts的邮件编码

while (<DATA>) {
    ($name, $phone, $address, $birth, $salary) = split(":", $_);
    ($street, $city, $capital) = split(",", $address);
    print $‘, "\n" if $address =~ /MA/;
}

__DATA__
Tommy Savage:408–724–0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66:34200
Lesle Kerstin:408–456–1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
JonDeLoach:408–253–3122:123 Park St., San Jose, CA 94086:7/25/53:85100
Ephram Hardy:293–259–5395:235 Carlton Lane, Joliet, IL 73858:8/12/20:56700
Betty Boop:245–836–8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
William Kopf:846–836–2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Norma Corder:397–857–2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
James Ikeda:834–938–8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Lori Gortz:327–832–5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Barbara Kerz:385–573–8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268500

运行结果为:

技术分享

本文出自 “天道酬勤” 博客,请务必保留此出处http://lavenliu.blog.51cto.com/5060944/1640384

Perl正则表达式--练习

标签:perl   正则表达式   练习   

原文地址:http://lavenliu.blog.51cto.com/5060944/1640384

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!