location of regular expression match

Discussion in 'Perl Misc' started by bob, Sep 15, 2003.

  1. bob

    bob Guest

    I apologize for another newbie question. How does one obtain the
    starting character position of all regular expression matches in a
    line or a paragraph?
    Thank you.
    bob, Sep 15, 2003
    #1
    1. Advertising

  2. bob

    Amir Kadic Guest

    bob wrote:

    > I apologize for another newbie question. How does one obtain the
    > starting character position of all regular expression matches in a
    > line or a paragraph?


    One way I can think of is using pos() in a loop:


    foreach $token (qw#foo bar#) {
    $position= pos($line),"\n" if $line =~ /$token/g;
    # now do something with it
    }

    I _hope_ there is a more elegant WTDI.

    Amir
    Amir Kadic, Sep 15, 2003
    #2
    1. Advertising

  3. bob

    Amir Kadic Guest

    I wrote:

    > $position= pos($line),"\n" if $line =~ /$token/g;


    Please forget the q(,"\n")

    This was originally a print(), but then I thought,
    'that man wants to _obtain_, not print the positions'...:)

    Amir
    Amir Kadic, Sep 15, 2003
    #3
    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. championsleeper
    Replies:
    6
    Views:
    1,003
    championsleeper
    Apr 6, 2004
  2. Liang
    Replies:
    2
    Views:
    1,676
  3. VSK
    Replies:
    2
    Views:
    2,287
  4. Replies:
    4
    Views:
    729
  5. Replies:
    0
    Views:
    364
Loading...

Share This Page