print "That was (${^PREMATCH})(${^MATCH})(${^POSTMATCH}).\n";
}
这些变量名看起来有点古怪,名字前面加上了脱字符
^
,又在外面围上了花括号。随
着
Perl
的进化,用于特殊事物的名字已经不敷使用,加上开头的
^
的目的是避免名
字冲突(实际上,程序员能够自由命名的变量是不能以
^
开头的,它是一个非法字
符),外面围住花括号的目的是表示其中的内容是完整的名字。
捕获变量(包括这里介绍的自动捕获变量以及之前介绍的有数字编号的那些)常常用
于正则表达式的替换操作。有关这一点,我们可以在第
9
章进一步学习。
优先级
学完正则表达式中这一堆元字符,你可能会觉得需要一张卡片帮助整理思路。这里给
出一张优先级表,说明模式中哪些部分的“紧密度”最高。不像操作符的优先级表,
正则表达式的优先级表相当简单,只有
4
个级别。我们这里顺便复习一下
Perl
模式中
使用的元字符。对于表
8-1
所展示的优先级顺序,大致阐释如下:
1.
在优先级表顶端的是括号
( )
,用于分组和捕获。圆括号里的东西的紧密度总是
比其他东西的更高。
2.
第二级是量词,也就是重复操作符:星号(
*
)、加号(
+
)、问号(
?
),以及
使用花括号表示的量词,比如
{5,15}
、
{3,}
和
{5}
。它们总是和它前面的条目
紧密相连。
3.
第三级是锚位和字符序列。我们已经介绍过的锚位有: ...
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.
O’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
I 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
I’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
I'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.