krebas krebas - 1 year ago 64
Perl Question

Regular expression matches too often

I have a string which contains certain class names + method calls. I'd like to extract these but the regex I wrote to do this returns more matches than intended.


$someInput = "Lorem ipsum dolor sit CLASS3.aMethod.anotherMethod amet, consetetur sadipscing CLASS1.bMethod elitr, sed diam nonumy eirmod";

@matches = ($someInput =~ /((CLASS1|CLASS2|CLASS3)(\.[A-Za-z0-9_]+){1,})/g);
foreach my $match (@matches) {
print $match . "\n";

Instead of one match in the form "CLASS.methodOne.methodTwo..." it also matches the class name and the methods separately. Please have a look at the example result:


What I actually want:


I would appreciate it if anyone could help me with this. A short explanation would also be great :) Thanks!

mob mob
Answer Source

The regular expression


defines 3 capture groups, each delineated by a balanced pair or parentheses:

1: ((CLASS1|CLASS2|CLASS3)(\.[A-Za-z0-9_]+){1,})

2: ((CLASS1|CLASS2|CLASS3)(\.[A-Za-z0-9_]+){1,})

3: ((CLASS1|CLASS2|CLASS3)(\.[A-Za-z0-9_]+){1,})

and so when one expression is successfully matched against this expression, the result will be 3 matches.

Use the (?:...) syntax to keep an expression together as a logical unit in a regular expression but to designate it as a non-capturing group. This expression will only return one match on success:

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