Storing submatches in an array?

E

ematiu

Hello list,

I have a regex in a variable, with an unknown number of submatches.
When I apply this regex to a string, how can I obtain all the
submatches in a array?

The submatches are returned in $1, $2 ..., but how can I know how many
are in total?

For example, if the number of submatches is 2:
$r='uid=(\d+).*groups=(\d+).*';
$str= `id`;
$str=~ /$r/;
@submatches=($1,$2);

but, now if
$r='uid=(\d+).*';
$str= `id`;
$str=~ /$r/;
@submatches=($1,$2); # how can I make this general??

Thanks!!

matías
 
U

usenet

(@submatches) = ($str =~ /$r/);
Note the paren's around the first term.

Actually, paren's aren't needed, since it's already an array. I was
thinking of usage like:
($var1, $var2) = ($str =~ /$r/);

in which paren's are needed.
 
S

Sherm Pendley

I have a regex in a variable, with an unknown number of submatches.
When I apply this regex to a string, how can I obtain all the
submatches in a array?

Just assign the results of the match to the array. The matching operator,
when used in list context, returns a list of matched sub-expressions instead
of a simple true/false scalar.
For example, if the number of submatches is 2:
$r='uid=(\d+).*groups=(\d+).*';
$str= `id`;
$str=~ /$r/;
@submatches=($1,$2);

my @submatches = $str =~ /$r/;

Have a look at "perldoc perlre" and/or "perldoc perlretut".

sherm--
 
J

James

Hello list,

I have a regex in a variable, with an unknown number of submatches.
When I apply this regex to a string, how can I obtain all the
submatches in a array?

The submatches are returned in $1, $2 ..., but how can I know how many
are in total?

my @a = split /$re/, $string;
print "@a";


James
 
P

Paul Lalli

James said:
my @a = split /$re/, $string;
print "@a";

That would return a list of all parts of $string that did NOT match
/$re/. I don't believe that's what the OP asked for.

Paul Lalli
 
J

John W. Krahn

I have a regex in a variable, with an unknown number of submatches.
When I apply this regex to a string, how can I obtain all the
submatches in a array?

The submatches are returned in $1, $2 ..., but how can I know how many
are in total?

For example, if the number of submatches is 2:
$r='uid=(\d+).*groups=(\d+).*';
$str= `id`;
$str=~ /$r/;
@submatches=($1,$2);

but, now if
$r='uid=(\d+).*';
$str= `id`;
$str=~ /$r/;
@submatches=($1,$2); # how can I make this general??

For the UID use the $< variable and for the GID use the $( variable. To get
the user name from the UID use getpwuid( $< ) in scalar context and to get the
group name from the GID use getgrgid( $( ) in scalar context.

perldoc perlvar
perldoc -f getpwuid
perldoc -f getgrgid



John
 

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,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top