Skip to Content
Perl语言入门(第8版)
book

Perl语言入门(第8版)

by Randal L. Schwartz, brian d foy, Tom Phoenix
October 2022
Beginner to intermediate
390 pages
7h 5m
Chinese
Southeast University Press
Content preview from Perl语言入门(第8版)
用正则表达式处理文本
185
$_ = "I'm much better\nthan Barney is\nat bowling,\nWilma.\n";
我们知道,
^
$
都是用于匹配整个字符串的开始和结尾的锚位(见第
8
章)。但
当模式加上
/m
修饰符之后,就可以用它们匹配字符串内的每一行(把
m
看作多行
multiple lines
)匹配会比较容易记住),这样一来,它们代表的位置就不再是整个
字符串的首尾,而是每行的开头跟结尾了。因此,下面的模式会成功匹配:
print "Found 'wilma' at start of line\n" if /^wilma\b/im;
同样地,你也可以对多行文本逐个进行替换。下面的代码先把整个文件读进一个变
量,然后在每行开头补充文件名:
open FILE, $filename
or die "Can't open '$filename': $!";
my $lines = join '', <FILE>;
$lines =~ s/^/$filename: /gm;
一次更新多个文件
通过程序自动更新文件内容时,最常见的做法就是先打开一个和原来内容一致的新文
件,然后在需要的位置进行改写,最后用修改后的文件替换原来的文件。这么做和直
接修改原始文件的效果大致相同,并且还有些额外好处。
比如下面这个例子,假设现在有几百个格式类似的文件。其中一个叫做
fred03.dat
其内容像下面这样:
Program name: granite
Author: Gilbert ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉
Perl语言入门

Perl语言入门

Randal L. Schwartz, brian d foy, Tom Phoenix

Publisher Resources

ISBN: 9787576602036