From: Peng Yu on
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/;

From: Dr.Ruud 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/;

Do you mean that your $string contains multiple lines?

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

--
Ruud
From: Peng Yu on
On Jun 11, 11:00 pm, "Dr.Ruud" <rvtol+use...(a)xs4all.nl> 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.
From: Dr.Ruud on
Peng Yu wrote:
> On Jun 11, 11:00 pm, "Dr.Ruud" <rvtol+use...(a)xs4all.nl> 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
From: Tad McClellan on
Peng Yu <pengyu.ut(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/;

----------------
#!/usr/bin/perl
use warnings;
use strict;

my $string = 'xxxxxxxsomepatternyyyyyyy';
print __LINE__, "\n" if $string =~ /xxxx(somepattern)yyyy/;
----------------



--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.