Hi all,
Here's my suggestion - tested it and works.
#!/usr/bin/perl
my $str = "Account \@id='1' or \@id='2' or \@id='3' or \@id='4'";
my $v = $str; #use if you need to keep $str intact
if ($v =~ s/\@id='(\d)'/$i{$1}=$1;/eg) {
print "1 = $i{'1'}\n" if defined $i{'1'};
print "2 = $i{'2'}\n" if defined $i{'2'};
print "3 = $i{'3'}\n" if defined $i{'3'};
print "4 = $i{'4'}\n" if defined $i{'4'};
}
However, can't get it to work if you store the regexpr in to $reg as in your
code. anyone know why???
Regards
Mark Kneen
----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, July 18, 2001 12:46 PM
Subject: Regex, how to capture within repetition?
> The script:
> >
> > #!perl -w
> > use strict;
> >
> > my $str = "Account \@id='1' or \@id='2' or \@id='3' or \@id='4'";
> >
> > my $reg = qr{(?:\@id='(\d+)'\s*or\s*)*\@id='(\d+)'};
> >
> > if ($str =~ $reg) {
> > print "Matched\n";
> > print "1 = $1\n" if defined($1);
> > print "2 = $2\n" if defined($2);
> > print "3 = $3\n" if defined($3);
> > print "4 = $4\n" if defined($4);
> > }
> >
> >
> > almost works, but only $1 and $2 contain captured values (3 and 4), how
do
> I
> > capture the first two ids in the repetition? (In the real case the
matched
> > string will contain any number, from 1, of "id='\+'" expresses, and I
want
> > all of them!)
> >
> > Richard Cox
> > Senior Software Developer
> > Dell Technology Online
> > All opinions and statements mine and do not in any way (unless expressly
> > stated) imply anything at all on behalf of my employer
> >
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]