user2585000 user2585000 - 5 months ago 11
Perl Question

perl regex character classes

i'm having trouble regex grouping in perl.

of course this is a much larger problem but it's the same concept i'm dealing with. thank you all in advance for your comments and ideas.

the regex below should only care about this portion of the string to make a decision.


doctor_who:ee

doctor_who:ep

doctor_who:ex


but not


doctor_who:eeh


code:

$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";
}

Answer

It's trivial with a negative lookahead zero-width assertion. This assumes the only thing you don't want to match specifically is doctor_who:eeh*:

/doctor_who:e(?!eh)[epx]/ 
Comments