正则表达式:匹配、锚定与修饰的全面指南
1. 正则表达式基础
1.1 重复次数限制
{n,m} 表示匹配必须至少出现 n 次且不能超过 m 次。示例代码如下:
my $one_to_three_a = qr/ca{1,3}t/;
like(
'cat',
$one_to_three_a, "'cat' matches /ca{1,3}t/"
);
like(
'caat',
$one_to_three_a, "'caat' matches /ca{1,3}t/"
);
like(
'caaat',
$one_to_three_a, "'caaat' matches /ca{1,3}t/"
);
unlike( 'caaaat', $one_to_three_a, "'caaaat' does not match /ca{1,3}t/" );
1.2 贪婪与非贪婪匹配
1.2.1 贪婪匹配
+ 和 * 本身是贪婪量词,它们会尽可能多地匹配输入字符串。例如:
my $hot_meal = qr/hot.*meal/;
say 'Found a hot meal!' if 'I have a hot meal' =~ $hot_meal;
say 'Found a ho
超级会员免费看
订阅专栏 解锁全文
10万+

被折叠的 条评论
为什么被折叠?



