regex to escape special characters 
Author Message
 regex to escape special characters

is there a regular expression to escape all non alphanumeric characters in a
string?

at the moment, i'm using s/\W//g to delete them, and s/\"/\\\"/g for
individual characters that i need to escape, but it all looks a bit pete
tong.

alex

(couldn't find a regex-dedicated group. is there one?)



Sat, 16 Jul 2005 19:38:51 GMT  
 regex to escape special characters

Quote:

> is there a regular expression to escape all non alphanumeric characters in a
> string?

See the description for \Q in the "Quote and Quote-like Operators"
section of:

   perldoc perlop

and also:

   perldoc -f quotemeta

Or, just do it all yourself (untested):

   s/(\W)/\\$1/g;

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sat, 16 Jul 2005 20:07:33 GMT  
 regex to escape special characters



Quote:
> is there a regular expression to escape all non alphanumeric characters in
a
> string?

> at the moment, i'm using s/\W//g to delete them, and s/\"/\\\"/g for
> individual characters that i need to escape, but it all looks a bit pete
> tong.

I am not QUITE sure what your problem is, maybe

    s/(\W)/\\$1/g

(But mind \n !!!)
There are a lot of things you can wrtite in the substitution part of s///e
when using the "execute" option.

Quote:
> (couldn't find a regex-dedicated group. is there one?)

There also are some books about Regex  even one especially on Perl (look at
Amazon e.g.)

Kindly Mike



Sat, 16 Jul 2005 20:14:18 GMT  
 regex to escape special characters

Quote:
> Or, just do it all yourself (untested):

>    s/(\W)/\\$1/g;

Genius - that works a treat. Thanks Tad. I had tried a similar pattern, but
omitted the brackets around the first \W.


Sat, 16 Jul 2005 20:17:59 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. escape special chars before loading into MySQL

2. is there a way to escape special chars

3. Newbie - Escaping special chars in forms

4. Formats: Escaping Characters

5. CGI hangs due to dodgy escape character

6. problems with the \n escape character

7. Escaping cursor characters

8. Variables and escaped characters in RegExp

9. how do I retranslate escaped characters

10. Striping VT100 escape characters from a file

11. Why do escape characters show when printing?

12. Which characters need escaping list?

 

 
Powered by phpBB® Forum Software