Prev: Converting HTML to PDF with Webkit
Next: FAQ 8.46 What's the difference between require and use?
From: Willem on 12 Jun 2010 05:19 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 13 Jun 2010 19:13 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 13 Jun 2010 19:15 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}
First
|
Prev
|
Pages: 1 2 Prev: Converting HTML to PDF with Webkit Next: FAQ 8.46 What's the difference between require and use? |