【发布时间】:2016-06-14 20:55:59
【问题描述】:
我在 perl 中的正则表达式分组遇到问题。
当然,这是一个更大的问题,但它与我正在处理的概念相同。提前感谢大家的 cmets 和想法。
下面的正则表达式应该只关心字符串的这一部分来做出决定。
doctor_who:ee
doctor_who:ep
doctor_who:ex
但不是
doctor_who:eeh
代码:
$str = "doctor_who:ee123ABC451234.123"; #match
$str = "doctor_who:ep123YXZ451234.123"; #match
$str = "doctor_who:ex123451234.123"; #match
$str = "doctor_who:eeh1234LMNOP51234.123"; ##should not match
$str = "doctor_who:abc12341234.123"; ##should not match
$regex = "doctor_who:e[e|p|x]"; #--->problem, what to add/remove?
if ($str =~ m/$regex/){
print "match!";
}
else {
print "not matched\n";
}
【问题讨论】:
-
注意:
[e|p|x]和[epx|]是一回事;它匹配这四个字符中的任何一个。你的意思是使用[epx]。