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

Discussion in 'Perl Misc' started by metfan, Jan 12, 2004.

  1. metfan

    metfan Guest

    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.
     
    metfan, Jan 12, 2004
    #1
    1. Advertising

  2. metfan

    gnari Guest

    "metfan" <qjzhupublic@___NoSpam__yahoo.ie> wrote in message
    news:eek:...
    > 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
     
    gnari, Jan 12, 2004
    #2
    1. Advertising

  3. metfan

    Ben Morrow Guest

    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, *
    it is worth reading when you are sixty." - C.S.Lewis
     
    Ben Morrow, Jan 12, 2004
    #3
  4. metfan

    dw Guest

    "metfan" <qjzhupublic@___NoSpam__yahoo.ie> wrote in message
    news:eek:...
    > 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";
    }
     
    dw, Jan 12, 2004
    #4
  5. metfan

    Bob Walton Guest

    gnari wrote:

    > "metfan" <qjzhupublic@___NoSpam__yahoo.ie> wrote in message
    > news:eek:...

    ....


    > 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
     
    Bob Walton, Jan 12, 2004
    #5
  6. metfan

    Anno Siegel Guest

    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
     
    Anno Siegel, Jan 12, 2004
    #6
  7. metfan

    gnari Guest

    "Bob Walton" <> wrote in message
    news:...
    > gnari wrote:
    >
    > > print (/$regexp/)[$index];

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


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

    gnari
     
    gnari, Jan 12, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cirfu
    Replies:
    1
    Views:
    239
    Carl Banks
    Jun 23, 2008
  2. Michael Tan
    Replies:
    32
    Views:
    1,082
    Ara.T.Howard
    Jul 21, 2005
  3. Jean Nibee
    Replies:
    4
    Views:
    213
    lasitha
    Apr 13, 2009
  4. Joao Silva
    Replies:
    16
    Views:
    409
    7stud --
    Aug 21, 2009
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    360
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page