find which subgroups don't match in regex

S

Shoryuken

Hello gents, here's the thing been confusing me for a while:

$regex="(\w+)\s([0-9]+)";

$a="Tom 1990"; # it's a match
$b="Jack xyz"; # not a match, because of $2 doesn't match ... but
here's my question, exactly how to inform the users of this unmatched
subgroup? (i.e. $2 is the problem, $1 is fine, etc.)

For a regex matching, is there a way to find which subgroups don't
match?

thanks in advance.
 
B

Ben Morrow

Quoth Shoryuken said:
Hello gents, here's the thing been confusing me for a while:

$regex="(\w+)\s([0-9]+)";

$a="Tom 1990"; # it's a match
$b="Jack xyz"; # not a match, because of $2 doesn't match ... but
here's my question, exactly how to inform the users of this unmatched
subgroup? (i.e. $2 is the problem, $1 is fine, etc.)

For a regex matching, is there a way to find which subgroups don't
match?

You can use /gc and \G to match one piece at a time, without losing your
place; something like

my @matches = qw/ \w+ \s [0-9]+ /;
my $string = 'Jack xyz';

for my $match (@matches) {
$string =~ /\G$match/gc
or print "$match failed at position " . pos $string;
}

Ben
 
X

xhoster

Shoryuken said:
Hello gents, here's the thing been confusing me for a while:

$regex="(\w+)\s([0-9]+)";

$a="Tom 1990"; # it's a match
$b="Jack xyz"; # not a match, because of $2 doesn't match ... but
here's my question, exactly how to inform the users of this unmatched
subgroup? (i.e. $2 is the problem, $1 is fine, etc.)

For a regex matching, is there a way to find which subgroups don't
match?

There isn't a built-in way. You'd have to build it yourself, and that
will probably be non-trivial, as it would pretty much have to be an expert
system in your exact context, not just some standard Perl feature.

For example, whose "fault" is it that this doesn't match:

"1990 Tom" =~ /(\w+)\s(\d+)/;

Both subgroups will match individually, just not when put together.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
S

Shoryuken

Quoth Shoryuken said:
Hello gents, here's the thing been confusing me for a while:
$regex="(\w+)\s([0-9]+)";

$a="Tom 1990"; # it's a match
$b="Jack xyz"; # not a match, because of $2 doesn't match ... but
here's my question, exactly how to inform the users of this unmatched
subgroup? (i.e. $2 is the problem, $1 is fine, etc.)
For a regex matching, is there a way to find which subgroups don't
match?

You can use /gc and \G to match one piece at a time, without losing your
place; something like

my @matches = qw/ \w+ \s [0-9]+ /;
my $string = 'Jack xyz';

for my $match (@matches) {
$string =~ /\G$match/gc
or print "$match failed at position " . pos $string;
}

Ben

This is a great idea, thanks!

And thanks the other guys for the good input, too!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top