Skip to Content
Perl语言入门
book

Perl语言入门

by Randal L. Schwartz, brian d foy, Tom Phoenix
August 2018
Beginner to intermediate
382 pages
7h 14m
Chinese
Southeast University Press
Content preview from Perl语言入门
134
7
后面谈到字符集简写时我们会看到更多
\N
的用法。
量词
我们可以用量词指定匹配项的重复次数。量词也是元字符,它把写在它之前的字符作
为要匹配的目标,然后定义它该重复匹配的次数。也有人称之为重复次数,或重复操
作符。
最简单的量词是问号
?
,它的意思是,前一个字符可以出现一次或干脆不出现(或
者说,前一个字符可有可无)。比如有的人写成
Bamm-bamm
,而有的人写成没有连字
符的
Bammbamm
,而你希望对两种写法都能匹配,那么这里的连字符
-
就是可有可无
的:
$_ = 'Bamm-bamm';
if (/Bamm-?bamm/) {
print "It matched!\n";
}
用你的测试程序试试看,以不同的方式输入
Bamm-Bamm
的名字会得到什么结果:
%
perl try_a_pattern "Bamm-?bamm"
Bamm-bamm
Matches
Bammbamm
Matches
Are you Bammbamm or Bamm-bamm?
Matches
仔细观察输入的最后一行,到底哪个版本得到了可以匹配的结论?
Perl
从目标字符串
最左端开始扫描,逐个判断当前位置上是否能达成匹配,直到发现符合条件的字符为
止,然后结束工作。所以这行匹配的是前一个版本,即
Bammbamm
。一旦
Perl
完成模
式匹配,就不再继续下去了,哪怕后面还有可以匹配的项。
Perl
总是匹配最左端能找
到的第一个子字符串,不管后续还有无匹配的,都不会改变当前结论,没必要做无用
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

计算机科学导论:跨学科方法

计算机科学导论:跨学科方法

罗伯特 塞奇威克, 凯文 韦恩
Perl语言入门(第8版)

Perl语言入门(第8版)

Randal L. Schwartz, brian d foy, Tom Phoenix

Publisher Resources

ISBN: 9787564177911