Help with pattern recognition

0 views
Skip to first unread message

Paul Spitalny

unread,
Jan 30, 2002, 1:58:16 PM1/30/02
to
Hi,
I have a file I am parsing through. It has the following line in it:

 19 v(in) voltage
 23 v(in.in) voltage
 

I want to find the first line (above) by searching on "v(in)". If I search on "in" I might get the wrong result since one line(the second one) has "in.in".

in my perl script I have a variable who's value is:
$my_variable = "v(in)";

when I go through the file and search for the line shown above by using:
open(IN,'<myfile.txt');
while ($_ =<IN>))
if ($_ =~ $my_variable))
  {print "I found it\n"}
close(IN);

I can't seem to find it. What's wrong?

ANy help appreciated.

Thanks,

Paul
 

Drew

unread,
Jan 30, 2002, 2:14:41 PM1/30/02
to
Paul Spitalny wrote:

[please post in plain text only!]

>
> in my perl script I have a variable who's value is:
> $my_variable = "v(in)";
>
> when I go through the file and search for the line shown above by
> using:

> open(IN,'<myfile.txt');

Get in the habit of checking for errors on open() and friends.

> while ($_ =<IN>))

^
This is overly verbose and wrong. Cut and paste next time, don't
re-write...

> if ($_ =~ $my_variable))
^
This is also wrong. Do you like parens or what?

> {print "I found it\n"}
> close(IN);
>


open(IN,'<myfile.txt') or die "can't open: $!";
while (<IN>)
{
if (/$my_variable/)
{


print "I found it\n"
}
}

close IN;

Paul Spitalny

unread,
Jan 30, 2002, 4:50:23 PM1/30/02
to
Drew wrote:

Hi Drew,
Thanks for your help. But, the problem seems to have something to do
with searching for text that has parentesis in it. The variable I am
searching for is 'v(in)' ala

$my_variable = "v(in)";

Then, if I use (as you suggest)....

if (/$my_variable/)

the if statement cannot seem to find the line of text that looks like
this....

19 v(in) voltage

Are the parentesis causing something unexpected (at least, unexpected to
me!) to happen??

Thanks!

Paul


Tassilo v. Parseval

unread,
Jan 30, 2002, 6:00:20 PM1/30/02
to
On Wed, 30 Jan 2002 13:50:23 -0800, Paul Spitalny wrote:

> $my_variable = "v(in)";
>
> Then, if I use (as you suggest)....
>
> if (/$my_variable/)
>
> the if statement cannot seem to find the line of text that looks like
> this....
>
> 19 v(in) voltage
>
> Are the parentesis causing something unexpected (at least, unexpected to
> me!) to happen??

Yes, they do. Parens are special characters in regexes. For such case
use the \E modifier:

if (/\E$my_variable\Q/)

where the \Q is actually optional here.
This will take any character inside the pattern verbosely and not treat
them as special.

Tassilo
--
$_=q!subJust{another()};subanother{Perl()};subPerl{hacker()};subhacker{map
{($_=(caller(3-$_))[3])=~s/main:://;$_}0..3}print"@{[Just()]},"!;s/su
b/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;

Tad McClellan

unread,
Jan 30, 2002, 6:03:43 PM1/30/02
to
Paul Spitalny <pa...@cascadelinear.com> wrote:
>Drew wrote:
>> Paul Spitalny wrote:


[ snip 40 lines ]


>Hi Drew,


Please do not quote an entire article. Trim it and leave just
enough to establish the context for you comments.


>But, the problem seems to have something to do
>with searching for text that has parentesis in it. The variable I am
>searching for is 'v(in)' ala
>
>$my_variable = "v(in)";
>
>Then, if I use (as you suggest)....
>
> if (/$my_variable/)


if (/\Q$my_variable/) # or: $my_variable = quotemeta $my_variable;


>Are the parentesis causing something unexpected (at least, unexpected to
>me!) to happen??


Yes, parenthesis are special in regular expressions.

perldoc perlre


--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas

Tad McClellan

unread,
Jan 30, 2002, 6:20:15 PM1/30/02
to
Tassilo v. Parseval <Tassilo....@post.rwth-aachen.de> wrote:

>Yes, they do. Parens are special characters in regexes. For such case
>use the \E modifier:
>
> if (/\E$my_variable\Q/)


The other way around:

if (/\Q$my_variable\E/)

Tassilo v. Parseval

unread,
Jan 30, 2002, 6:24:37 PM1/30/02
to
On Wed, 30 Jan 2002 23:20:15 GMT, Tad McClellan wrote:
> Tassilo v. Parseval <Tassilo....@post.rwth-aachen.de> wrote:

>> if (/\E$my_variable\Q/)
>
>
> The other way around:
>
> if (/\Q$my_variable\E/)

Hmmh, must be a virus tormenting Germany remembering Find::File in a
previous post not long ago. :-)

Paul Spitalny

unread,
Jan 30, 2002, 7:50:30 PM1/30/02
to
Hi Everybody,
Thanks for all the help!

The "\Q" definitely did the trick!!


Thanks again,

Paul

Tina Mueller

unread,
Jan 31, 2002, 9:10:06 PM1/31/02
to
Tassilo v. Parseval <Tassilo....@post.rwth-aachen.de> wrote:
> On Wed, 30 Jan 2002 23:20:15 GMT, Tad McClellan wrote:
>> Tassilo v. Parseval <Tassilo....@post.rwth-aachen.de> wrote:

>>> if (/\E$my_variable\Q/)
>>
>> The other way around:
>>
>> if (/\Q$my_variable\E/)

> Hmmh, must be a virus tormenting Germany remembering Find::File in a
> previous post not long ago. :-)

and me saying use /^0?[1-9]+$/ for recognizing integers
withor without a leading zero... =)
a heard the flu is coming to germany...

--
http://tinita.de \ enter__| |__the___ _ _ ___
PerlQuotes: http://tinita.de/pq.html \ / _` / _ \/ _ \ '_(_-< of
MovieDB: http://tinita.de/tmdb.html \ \ _,_\ __/\ __/_| /__/ perception

Reply all
Reply to author
Forward
0 new messages