From: Willem on
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
From: C.DeRykus on
On Jun 11, 8:57 pm, Peng Yu <pengyu...(a)gmail.com> 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
From: C.DeRykus on
On Jun 13, 4:13 pm, "C.DeRykus" <dery...(a)gmail.com> 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}