11、正则表达式:匹配、锚定与修饰的全面指南

正则表达式:匹配、锚定与修饰的全面指南

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个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值