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语言入门
140
7
$_ = "aa11bb";
if (/(.)\g{–1}11/) {
print "It matched!\n";
}
在这个基础上,如果再加一个捕获分组,会改变原来的分组编号,使用绝对编号书写
的模式可能需要重新确认配对顺序。而使用相对编号的反向引用则不用改动,它只关
心它左侧从近到远的分组,只要这个相对距离不变,编号就不用变。所以增加一个分
组后,原来的编号照旧:
use v5.10;
$_ = "xaa11bb";
if (/(.)(.)\g{–1}11/) {
print "It matched!\n";
}
择一匹配
模式中的竖线字符
|
,我们也叫它“或”,表示择一匹配,要么左侧部分匹配,要么
右侧部分匹配。如果左侧模式匹配失败,那就尝试右侧模式:
foreach ( qw(fred betty barney dino) ) {
if ( /fred|barney/ ) {
print "$_ matched\n";
}
}
这段程序会打印两个名字,第一次是左侧部分,第二次是右侧部分:
fred
barney
当然,我们可以提供更多选项:
foreach ( qw(fred betty barney dino) ) {
if ( /fred|barney|betty/ ) {
print "$_ matched\n";
}
}
于是打印三个名字:
fred
betty
barney
正则表达式
141
竖线会把模式切成数块,有时候不是我们想要的效果。比如要寻找姓
Flintstones ...
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