How to return the line number that right next to a match?

Discussion in 'Perl Misc' started by Peng Yu, Jun 12, 2010.

  1. Peng Yu

    Peng Yu Guest

    After I did a regex match, I want to figure out what the line number
    is after the match in (). Is there an easy way to do it?

    $string =~ /xxxx(somepattern)yyyy/;
    Peng Yu, Jun 12, 2010
    #1
    1. Advertising

  2. Peng Yu

    Dr.Ruud Guest

    Peng Yu wrote:

    > After I did a regex match, I want to figure out what the line number
    > is after the match in (). Is there an easy way to do it?
    >
    > $string =~ /xxxx(somepattern)yyyy/;


    Do you mean that your $string contains multiple lines?

    Otherwise see $. in perlvar, as you have been told before.

    --
    Ruud
    Dr.Ruud, Jun 12, 2010
    #2
    1. Advertising

  3. Peng Yu

    Peng Yu Guest

    On Jun 11, 11:00 pm, "Dr.Ruud" <> wrote:
    > Peng Yu wrote:
    > > After I did a regex match, I want to figure out what the line number
    > > is after the match in ().  Is there an easy way to do it?

    >
    > > $string =~ /xxxx(somepattern)yyyy/;

    >
    > Do you mean that your $string contains multiple lines?


    Yes. $string contains multiple lines.
    Peng Yu, Jun 12, 2010
    #3
  4. Peng Yu

    Dr.Ruud Guest

    Peng Yu wrote:
    > On Jun 11, 11:00 pm, "Dr.Ruud" <> wrote:
    >> Peng Yu wrote:


    >>> After I did a regex match, I want to figure out what the line number
    >>> is after the match in (). Is there an easy way to do it?
    >>> $string =~ /xxxx(somepattern)yyyy/;

    >>
    >> Do you mean that your $string contains multiple lines?

    >
    > Yes. $string contains multiple lines.


    So how did they end up there, did you slurp a file into it?
    If so, then why not just process the file line-by-line?


    You can still use a while-loop on the $string, for example:

    my $n = 0;
    while ( my ($line) = $string =~ /.*/g ) {
    ++$n;
    print "$n ($1)\n" if $line =~ /xxxx(somepattern)yyyy/;
    }

    --
    Ruud
    Dr.Ruud, Jun 12, 2010
    #4
  5. Peng Yu

    Willem Guest

    Peng Yu wrote:
    ) After I did a regex match, I want to figure out what the line number
    ) is after the match in (). Is there an easy way to do it?
    )
    ) $string =~ /xxxx(somepattern)yyyy/;

    Just count the newlines in the substring up to the (somepattern) capture.

    my $linenr = substr($string, 0, $+[-1]) =~ tr/\n//;

    See 'perldoc perlvar' for an explanation of the @+ array.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Jun 12, 2010
    #5
  6. Peng Yu

    C.DeRykus Guest

    On Jun 11, 8:57 pm, Peng Yu <> wrote:
    > After I did a regex match, I want to figure out what the line number
    > is after the match in ().  Is there an easy way to do it?
    >
    > $string =~ /xxxx(somepattern)yyyy/;



    The pre-match variable greatly simplifies the
    solution, and with 5.10's /p switch, doesn't
    incur a global performance penalty:

    if ( $string =~ /somepattern/p ) {
    my $linenum = {^PREMATCH} =~ tr/\n//;
    }

    --
    Charles DeRykus
    C.DeRykus, Jun 14, 2010
    #6
  7. Peng Yu

    C.DeRykus Guest

    On Jun 13, 4:13 pm, "C.DeRykus" <> wrote:
    > ...
    >
    > The pre-match variable greatly simplifies the
    > solution, and with 5.10's /p switch, doesn't
    > incur a global performance penalty:
    >
    > if ( $string =~ /somepattern/p ) {
    >    my $linenum = {^PREMATCH} =~ tr/\n//;

    ^^^^^^^^^^^
    ${^PREMATCH}
    C.DeRykus, Jun 14, 2010
    #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. Dave Sisk
    Replies:
    3
    Views:
    656
    Jim Gibson
    Mar 5, 2004
  2. Hugo
    Replies:
    10
    Views:
    1,292
    Matt Humphrey
    Oct 18, 2004
  3. news.frontiernet.net
    Replies:
    3
    Views:
    10,382
    Barry Pearson
    Apr 18, 2004
  4. Deniz Bahar
    Replies:
    2
    Views:
    451
    Andrey Tarasevich
    Mar 9, 2005
  5. Pea
    Replies:
    12
    Views:
    479
    John W. Krahn
    Aug 13, 2004
Loading...

Share This Page