CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Escape characters in a scalar

 



d1zz13
User

May 18, 2005, 10:54 AM

Post #1 of 8 (6405 views)
Escape characters in a scalar Can't Post

I'm tring to escape characters in a scalar and wonder if it's possible.

I have the following:

Code
$var = "ORA-02260"; 
$var2 = "ORA-00955: name is already used by an existing object";

if ($var2 =~ /^[^$var]/){
push @errors $var2;
}


Logically what I'm trying to say is if ORA-02260 doesn't appear at the beginning of my string then put it into @errors.

My problem is that when the regex gets to the hyphen (in ORA-02260) it warns that A-0 is not a valid class. It's not meant to be a class, I'm only using the square brackets to negate it.

Any ideas on how I can escape the hyphen in the variable?


KevinR
Veteran


May 18, 2005, 11:25 AM

Post #2 of 8 (6404 views)
Re: [d1zz13] Escape characters in a scalar [In reply to] Can't Post

use the \Q...\E operators to escape meta characters in the search string:


Code
if ($var2 =~ /^\Q$var\E/){


see if that helps
-------------------------------------------------


d1zz13
User

May 18, 2005, 12:05 PM

Post #3 of 8 (6402 views)
Re: [KevinR] Escape characters in a scalar [In reply to] Can't Post

Thanks KevinR,

Is there a list of 'operators and what they do' somewhere that I can reference?

(This post was edited by d1zz13 on May 18, 2005, 12:23 PM)


d1zz13
User

May 18, 2005, 12:33 PM

Post #4 of 8 (6399 views)
Re: [d1zz13] Escape characters in a scalar [In reply to] Can't Post

Don't worry, I found something about it at PerlDoc


d1zz13
User

May 18, 2005, 12:48 PM

Post #5 of 8 (6396 views)
Re: [KevinR] Escape characters in a scalar [In reply to] Can't Post

KevinR,

I've just realised that the code you've given me only pushes the variable into the array if it does match, I wanted it to push it in only if it doesn't match, that's why I was trying to negate it.

Any more ideas?


KevinR
Veteran


May 18, 2005, 2:42 PM

Post #6 of 8 (6394 views)
Re: [d1zz13] Escape characters in a scalar [In reply to] Can't Post


Code
if ($var2 !~ /^\Q$var\E/){


which reads: if $var2 does not match /^\Q$var\E/

!~ is the compliment of =~

or you could use "unless" and "=~"


Code
unless ($var2 =~ /^\Q$var\E/){


which basicaly does the same thing as above but might be easier to understand in a grammatic sense:

unless $var2 matches /^\Q$var\E/

sometimes one or the other is easier to apply to a particular condition. In your case either one will work fine as far as I can tell.
-------------------------------------------------


davorg
Thaumaturge / Moderator

May 19, 2005, 5:35 AM

Post #7 of 8 (6390 views)
Re: [KevinR] Escape characters in a scalar [In reply to] Can't Post

Of course, the hyphen is only a metacharacter within a character class so now you've removed the (unnecessary) character class there's not need to escape the hyphen :)

But escaping variables intepolated into regexes is generally a good idea.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


KevinR
Veteran


May 19, 2005, 11:50 AM

Post #8 of 8 (6389 views)
Re: [davorg] Escape characters in a scalar [In reply to] Can't Post

err.... I knew that Blush
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives