Regular Expression $1?

Regular Expression $1?

am 10.10.2007 03:12:36 von Yang

I want to get everything between () in the file, say (5), (6), so I
wrote the following:

while(my $line = ){
chomp $line;
if($line =~ /\(\d*\)/{
print $1;
}
}

But it always returns error saying that $1 is uninitialized.

Re: Regular Expression $1?

am 10.10.2007 03:40:42 von Jim Cochrane

On 2007-10-10, Yang wrote:
> I want to get everything between () in the file, say (5), (6), so I
> wrote the following:
>
> while(my $line = ){
> chomp $line;
> if($line =~ /\(\d*\)/{
> print $1;
> }
> }
>
> But it always returns error saying that $1 is uninitialized.
>

I think you want:

if ($line =~ /(\(\d+\))/ {
....
}


[CE: not tested.]

--

Re: Regular Expression $1?

am 10.10.2007 09:16:48 von Josef Moellers

Jim Cochrane wrote:
> On 2007-10-10, Yang wrote:
>=20
>>I want to get everything between () in the file, say (5), (6), so I
>>wrote the following:
>>
>>while(my $line =3D ){
>> chomp $line;
>> if($line =3D~ /\(\d*\)/{
>> print $1;
>> }
>>}
>>
>>But it always returns error saying that $1 is uninitialized.
>>
>=20
>=20
> I think you want:
>=20
> if ($line =3D~ /(\(\d+\))/ {
> ...
> }

ITYM "/\((\d*)\)/"

Yang wanted "everything *between* ()".

--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

Re: Regular Expression $1?

am 10.10.2007 12:51:11 von Paul Lalli

On Oct 9, 9:12 pm, Yang wrote:
> I want to get everything between () in the file, say (5), (6), so I
> wrote the following:
>
> while(my $line = ){
> chomp $line;
> if($line =~ /\(\d*\)/{
> print $1;
> }
>
> }
>
> But it always returns error saying that $1 is uninitialized.

$1 is assigned to whatever is "captured" by parentheses in the pattern
match. You have no capturing parentheses in the pattern match. You
only have the litteral parenthses that you're trying to match. You
need to surround what you want to capture with parentheses:

/\((\d*)\)/

Note that \d* means "0 or more digits" which means an empty () is a
valid match for this expression. If you want to require at least one
digit, change the * to a +.

Paul Lalli

Re: Regular Expression $1?

am 10.10.2007 17:55:04 von Cloink_Friggson

Paul - well done for answering lucidly, it doesn't happen often, esp
not on Perl pages.

Re: Regular Expression $1?

am 11.10.2007 02:54:05 von Tad McClellan

Cloink wrote:

> Paul - well done for answering lucidly, it doesn't happen often, esp
> not on Perl pages.


It is likely that the results you have observed correlate
with the attitude that you have displayed here in the past.

Insulting people does not motivate them to be helpful.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Regular Expression $1?

am 16.10.2007 20:47:39 von Jeff Stampes

Yang wrote:
> I want to get everything between () in the file, say (5), (6), so I
> wrote the following:

Not exactly, the following doesn't compile.

> while(my $line = ){
> chomp $line;
> if($line =~ /\(\d*\)/{
> print $1;
> }
> }
>
> But it always returns error saying that $1 is uninitialized.

It would be helpful to provide an example of your data as well.

IAC, I won't give you any fish, but here's a line and a pole:

You get the capture variables ($1, $2, $3, etc) by capturing things in
sets of parentheses. Where are the parentheses in your regexp that
would be doing the capturing?

~Jeff