pos and capture

Discussion in 'Perl Misc' started by UToronto News, Aug 10, 2004.

  1. I have a question about capturing information from a regular expression
    match

    when i run

    $string="the rain in spain falls mainly on the plain";
    $searchpattern=".ain";

    while ($string=~m/$searchpattern/g)
    {
    $position=pos($string)-length($search);
    print "position=$position\n";
    pos($string)=$position+1;
    }

    -- this searches for all occurences of $search in $string, and prints out
    the starting position of each
    position=8
    position=17
    position=28
    position=43


    but when i run
    while (($capture)=$string=~m/($searchpattern)/g)
    {
    $position=pos($string)-length($searchpattern);
    print "position=$position : $capture\n";
    pos($string)=$position+1;
    }

    -- I am able to capture the match, but pos($string) is empty, resulting in
    $position equal to 0-length($capture)
    position=-4 : rain

    When capturing the match, does the pos pointer reset? Is there a way of
    using this pointer with capture?

    Thanks
    Larry
     
    UToronto News, Aug 10, 2004
    #1
    1. Advertising

  2. UToronto News

    LEH Guest

    I sorted out how to get this working.
    instead of capturing into a variable, I capture into the $1 and use that

    ie
    while($string=~/($searchpatten)/g)
    {
    $position=pos($string)-length($searchpattern);
    print "position=$position: $1\n";
    pos($string)=$position+1;
    }

    this does work, but I am still curious to know why my capturing into a
    variable resets pos($string)

    Larry








    "UToronto News" <> wrote in message
    news:...
    >
    > I have a question about capturing information from a regular expression
    > match
    >
    > when i run
    >
    > $string="the rain in spain falls mainly on the plain";
    > $searchpattern=".ain";
    >
    > while ($string=~m/$searchpattern/g)
    > {
    > $position=pos($string)-length($search);
    > print "position=$position\n";
    > pos($string)=$position+1;
    > }
    >
    > -- this searches for all occurences of $search in $string, and prints out
    > the starting position of each
    > position=8
    > position=17
    > position=28
    > position=43
    >
    >
    > but when i run
    > while (($capture)=$string=~m/($searchpattern)/g)
    > {
    > $position=pos($string)-length($searchpattern);
    > print "position=$position : $capture\n";
    > pos($string)=$position+1;
    > }
    >
    > -- I am able to capture the match, but pos($string) is empty, resulting in
    > $position equal to 0-length($capture)
    > position=-4 : rain
    >
    > When capturing the match, does the pos pointer reset? Is there a way of
    > using this pointer with capture?
    >
    > Thanks
    > Larry
    >
    >
    >
     
    LEH, Aug 10, 2004
    #2
    1. Advertising

  3. [posted & mailed]

    On Tue, 10 Aug 2004, LEH wrote:

    >this does work, but I am still curious to know why my capturing into a
    >variable resets pos($string)
    >
    >> while (($capture)=$string=~m/($searchpattern)/g)


    The problem is that saying

    (...) = $string =~ /pattern/g;

    enforces *list* context on the pattern match, and a global pattern match
    in list context matches as many times as possible, and afterwards, pos()
    doesn't have a useful value.

    --
    Jeff "japhy" Pinyan % How can we ever be the sold short or
    RPI Acacia Brother #734 % the cheated, we who for every service
    RPI Corporation Secretary % have long ago been overpaid?
    http://japhy.perlmonk.org/ %
    http://www.perlmonks.org/ % -- Meister Eckhart
     
    Jeff 'japhy' Pinyan, Aug 10, 2004
    #3
  4. UToronto News

    Anno Siegel Guest

    UToronto News <> wrote in comp.lang.perl.misc:
    >
    > I have a question about capturing information from a regular expression
    > match
    >
    > when i run


    ....without strictures and warnings, it seems.

    > $string="the rain in spain falls mainly on the plain";
    > $searchpattern=".ain";
    >
    > while ($string=~m/$searchpattern/g)
    > {
    > $position=pos($string)-length($search);

    ^^^^^^^
    There is no variable "$search". "strict" would have told you so.
    Also, the length of the search pattern is generally not the same
    as the length of the match. Use the @- array for the beginning
    of a match.

    > print "position=$position\n";
    > pos($string)=$position+1;


    This is an error. pos( $string) points already one character past
    the match. Incrementing it further may miss some.

    > }
    >
    > -- this searches for all occurences of $search in $string, and prints out
    > the starting position of each
    > position=8
    > position=17
    > position=28
    > position=43


    Have you checked that? Your positions are too large by 4:

    print "position=$-[ 0]\n" while $string =~ /$searchpattern/g;

    position=4
    position=13
    position=24
    position=39

    Your question about pos() after a match in list context has been answered.

    Anno
     
    Anno Siegel, Aug 11, 2004
    #4
    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. Max
    Replies:
    7
    Views:
    9,119
  2. Clint Olsen
    Replies:
    6
    Views:
    361
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  3. Replies:
    6
    Views:
    118
    Fabian Pilkowski
    Mar 17, 2005
  4. se
    Replies:
    19
    Views:
    181
  5. prati
    Replies:
    0
    Views:
    447
    prati
    Oct 27, 2012
Loading...

Share This Page