user2585000 user2585000 - 2 years ago 74
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.




but not



$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 Source

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*:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download