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.
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.