Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   [regexp] How to access the matched result by index? (http://www.velocityreviews.com/forums/t884570-regexp-how-to-access-the-matched-result-by-index.html)

metfan 01-12-2004 02:40 AM

[regexp] How to access the matched result by index?
 
Hi, gurus,

We know that while using regexp to match something, the matched
result will be set in $1, $2, $3, $4..... but, is there a way to
access the result by an index? for example I've a variable called
$index, if has an integer of 10, so the result I need is $10, so
is there any array for me to use like $MatchedResult[$index]?
thanks so much.

gnari 01-12-2004 02:50 AM

Re: [regexp] How to access the matched result by index?
 
"metfan" <qjzhupublic@___NoSpam__yahoo.ie> wrote in message
news:opr1m5pkhdrp6n8a@news.yaako.com...
> Hi, gurus,
>
> We know that while using regexp to match something, the matched
> result will be set in $1, $2, $3, $4..... but, is there a way to
> access the result by an index? for example I've a variable called
> $index, if has an integer of 10, so the result I need is $10, so
> is there any array for me to use like $MatchedResult[$index]?
> thanks so much.


apart from the $1,$2... thing,

there is the very practical
($a,$b,$c)=/(.)(..)(..)/;

so you can do
@MatchedResult=/$regexp/;
print $MatchedResult[$index];

or just
print (/$regexp/)[$index];

gnari




Ben Morrow 01-12-2004 03:03 AM

Re: [regexp] How to access the matched result by index?
 

metfan <qjzhupublic@___NoSpam__yahoo.ie> wrote:
> Hi, gurus,
>
> We know that while using regexp to match something, the matched
> result will be set in $1, $2, $3, $4..... but, is there a way to
> access the result by an index? for example I've a variable called
> $index, if has an integer of 10, so the result I need is $10, so
> is there any array for me to use like $MatchedResult[$index]?
> thanks so much.


See the example in perldoc perlvar under @-.

Ben

--
"If a book is worth reading when you are six, * ben@morrow.me.uk
it is worth reading when you are sixty." - C.S.Lewis

dw 01-12-2004 03:16 AM

Re: [regexp] How to access the matched result by index?
 

"metfan" <qjzhupublic@___NoSpam__yahoo.ie> wrote in message
news:opr1m5pkhdrp6n8a@news.yaako.com...
> Hi, gurus,
>
> We know that while using regexp to match something, the matched
> result will be set in $1, $2, $3, $4..... but, is there a way to
> access the result by an index? for example I've a variable called
> $index, if has an integer of 10, so the result I need is $10, so
> is there any array for me to use like $MatchedResult[$index]?
> thanks so much.


How about using:

$_ = "abcdefghijklmnopqrstuvwxyz";

@MatchedResult = /(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)/;

for ($count=0; $count<@MatchedResult; $count++) {
print "\$MatchedResult[$count]: $MatchedResult[$count]\n";
}



Bob Walton 01-12-2004 03:36 AM

Re: [regexp] How to access the matched result by index?
 
gnari wrote:

> "metfan" <qjzhupublic@___NoSpam__yahoo.ie> wrote in message
> news:opr1m5pkhdrp6n8a@news.yaako.com...

....


> print (/$regexp/)[$index];



Um, that's a syntax error. Maybe you mean:

print +(/$regexp/)[$index];


>
> gnari


--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl


Anno Siegel 01-12-2004 09:25 AM

Re: [regexp] How to access the matched result by index?
 
metfan <qjzhupublic@___NoSpam__yahoo.ie> wrote in comp.lang.perl.misc:
> Hi, gurus,
>
> We know that while using regexp to match something, the matched
> result will be set in $1, $2, $3, $4..... but, is there a way to
> access the result by an index? for example I've a variable called
> $index, if has an integer of 10, so the result I need is $10, so
> is there any array for me to use like $MatchedResult[$index]?
> thanks so much.


If you know the number of possible matches, the simplest way is

( $1, $2, $3, $4, $5)[ $i];

The simplest isn't necessarily the best way.

Anno

gnari 01-12-2004 09:39 AM

Re: [regexp] How to access the matched result by index?
 
"Bob Walton" <invalid-email@rochester.rr.com> wrote in message
news:40021615.1010203@rochester.rr.com...
> gnari wrote:
>
> > print (/$regexp/)[$index];

>
> Um, that's a syntax error. Maybe you mean:
>
> print +(/$regexp/)[$index];


actually, i meant print ((/$regexp/)[$index]);

gnari






All times are GMT. The time now is 02:35 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.