[VPM] regular expression line number in string

Peter Scott Peter at psdt.com
Fri Mar 3 18:22:49 PST 2006


At 6:13 PM -0800 3/2/06, Jeremy Aiyadurai wrote:
>hello all perl guru's
>
>in a regular expression how do I retrieve the line index/number of a match
>in a multiline(\r\n) string?
>
>eg.
>-------------------
>
>my $line = "asdasdasdasdasdasdasdasdasdasdasdasdasdasdasd
>asdsadasdasdasdadasdasdasdasdasdasdhereaSDASDAS
>asdasdasdasdasdasdasdasdasdasdasdasdasdasdasd";
>
>$line =~ m/(here)/ig;

If the pattern you are matching on does not contain newlines, then I 
would do (untested):

for (my $count = 0; $line =~ /(.*\r?\n?)/g; $count++) {
   print "Found at line $count\n" and last if $1 =~ /here/i;
}


More information about the Victoria-pm mailing list