Brackets () in variable used for pattern match

M

Marcus Brody

Hi,

I'm new to perl and programming, so please please dont throw the
manual at me!!

Im trying to do a pattern match e.g.

if $foo =~ m/$bar/

However, on occasions $bar contains something like "HMGI(Y)", and the
brackets are intefering with the pattern match. In all other
instances my script works. I presume the brackets are being
extrapolated such that my pattern match *essentially* becomes:

if "HMGI(Y)" =~ m/HMGI(Y)/

Which clearly wont work, as the brackets are only used to capture Y in
the variable $1, and are otherwise "ignored". Is there a way to
"escape" things in variables, even though I dont know whether they are
going to contain a bracket or not??


Thanks in advance


MB
 
A

Anno Siegel

Marcus Brody said:
Hi,

I'm new to perl and programming, so please please dont throw the
manual at me!!

Im trying to do a pattern match e.g.

if $foo =~ m/$bar/

However, on occasions $bar contains something like "HMGI(Y)", and the
brackets are intefering with the pattern match. In all other
instances my script works. I presume the brackets are being
extrapolated such that my pattern match *essentially* becomes:

if "HMGI(Y)" =~ m/HMGI(Y)/

Which clearly wont work, as the brackets are only used to capture Y in
the variable $1, and are otherwise "ignored". Is there a way to
"escape" things in variables, even though I dont know whether they are
going to contain a bracket or not??

Look up quotemeta(). Use \Q.

Anno
 
T

Thens

William Hymen said:
I just solved the problem yesterday for brackets. Funny you should ask.
I did this substitute before the match. It's not very elegant, but it works
by matching ANY character using the period in the same position
as the () charachters were.

[ snipped a bad example ]

Please do not top post. Quote only what is needed and post your reply
at the bottom of the quote.

Back to the problem, your solution is not generic. When perl provides
you facilities to Quote the meta characters in a regex, why reinvent the
wheel that too badly. use \Q to quote.

perl -e 'print "\QABCD(Y)"'

to see what happens when you use \Q.

Regards,
Thens.
 
T

Thens

On 3 Sep 2003 02:09:57 -0700
Hi,

I'm new to perl and programming, so please please dont throw the
manual at me!!

You will remain a newbie, if you dont develop a liking for the manual
;-).

Manual is the best source of reference, what more it is available in
your hard disk. What are you waiting for.

perldoc -f <fucntion>
perldoc -q <keyword> # faq search
perldoc perldoc # to know about the manual
perldoc perldata # perl data structures
perldoc perlre # Regular expressions
perldoc perlref # references
- more-

Have fun !!

Regards,
Thens.
 
M

Marcus Brody

I just solved the problem yesterday for brackets. Funny you should ask.
I did this substitute before the match. It's not very elegant, but it works
by matching ANY character using the period in the same position
as the () charachters were.

$save=$bar;
$save =~ s|\(|\.|g;
$save =~ s|\)|\.|g;
if $foo =~ m/$save/


Hey

Just came to pretty much the same conclusion myself ;-)
instead, I quoted the offending characters rather than exchanging them:

e.g.
$save =~ s|\(|\\\(|g;
etc.

(with the "\\" equally an escaped "\" and the "\(" equalling an escaped "(" )

This is pretty much the same as quotemeta, so I shall use that instead :)

Thanks peoples

MB
 
J

John Bokma

William said:
Sorry, never knew there was a rule called top-posting.

A rule *against*
I get tired of constantly scrolling to the bottom ;) - Bill

We get tired of scrolling down to understand wtf a top poster is
replying to. Also We are amazed at the garbage that hasn't been cut out.
Cut pieces no longer needed and you don't have to scroll. And your
replies will be much more understandable.

BTW tinw
 
T

Tad McClellan

William Hymen said:
I get tired of constantly scrolling to the bottom ;) - Bill


You do not need to scroll to the bottom when followups are
composed properly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top