get the number of subgroups in a regex

S

Sara

Hi,

Is there any way to get the number of subgroups in the regular
expression "inline", in the regex itself?

# start of code block
use strict;
use warnings;

my $string = 'foo bar baz, will match this or not';

my $regex1 = '(\b\w{3}\b)';
my $regex2 = '(\b\w{3} (\w{3})\b)';
my $regex3 = '(\b\w{4,}\b)';

foreach my $reg ($regex1, $regex2, $regex3) {
my @matches = $string =~ /$reg/g;
my $num = @matches;
print "\nString: $string\n";
print " regex: $reg\n";
print " matches: ",join(', ',@matches),"\n";
print " number of matches: $num\n";
}
# end of code block

This works as expected, but the following doesn't work.

my $num;
$string =~ s/$reg(?{ $num=$#+ })/Replace($num,$string)/eg;

It says "Eval-group not allowed at runtime, ...". The perlre page
mentions that you can use $#+ to determine how many subgroups were in
the last successful match. It would be very helpful if someone can
explain how exactly this code inside regex works.

Can someone please help.

Thanks,
Sara.
 
C

ced

Sara said:
Hi,

Is there any way to get the number of subgroups in the regular
expression "inline", in the regex itself?

# start of code block
use strict;
use warnings;

my $string = 'foo bar baz, will match this or not';

my $regex1 = '(\b\w{3}\b)';
my $regex2 = '(\b\w{3} (\w{3})\b)';
my $regex3 = '(\b\w{4,}\b)';

foreach my $reg ($regex1, $regex2, $regex3) {
my @matches = $string =~ /$reg/g;
my $num = @matches;
print "\nString: $string\n";
print " regex: $reg\n";
print " matches: ",join(', ',@matches),"\n";
print " number of matches: $num\n";
}
# end of code block

This works as expected, but the following doesn't work.

my $num;
$string =~ s/$reg(?{ $num=$#+ })/Replace($num,$string)/eg;

It says "Eval-group not allowed at runtime, ...". The perlre page
mentions that you can use $#+ to determine how many subgroups were in
the last successful match. It would be very helpful if someone can
explain how exactly this code inside regex works


You'll need to add: use re 'eval'
From the re docs:

When "use re 'eval'" is in effect, a regex is allowed to
contain "(?{ ... })" zero-width assertions even if regular
expression contains variable interpolation. That is
normally disallowed, since it is a potential security risk.
Note that this pragma is ignored when the regular expression
is obtained from tainted data, i.e. evaluation is always
disallowed with tainted regular expresssions. See the
section on "(?{ code })" in the perlre manpage.

For the purpose of this pragma, interpolation of precompiled
regular expressions (i.e., the result of "qr//") is not
considered variable interpolation.

....
 

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
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top