Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Re: Make a string regexp-safe

by mr2 (Friar)
on Jul 14, 2002 at 17:32 UTC ( #181602=note: print w/ replies, xml ) Need Help??


in reply to Re: Make a string regexp-safe
in thread Make a string regexp-safe

one way : s/([\&;\`'\\\|"*?~<>^\(\)\[\]\{\}\$\n\r])/\\$1/g;

Replies are listed 'Best First'.
Re: Re: Re: Make a string regexp-safe
by IlyaM (Parson) on Jul 14, 2002 at 18:50 UTC
    It is very ugly and it is not very error prone (who knows what changes in regexp syntax can be expected in perl 5.10.0). It is better either to use already mentioned quotemeta or to escape string with \Q and \E in regexp. Example:
    my $string = "some unsafe chars"; .... =~ /\Q$string\E/;
    It is exactly equivalent to
    my $string = "some unsafe chars"; my $temp = quotemeta $string; .... =~ /$temp/;
    See perlre for more info.

    --
    Ilya Martynov (http://martynov.org/)

Re: Re: Re: Make a string regexp-safe
by pm5 (Acolyte) on Jul 14, 2002 at 18:10 UTC

    thanks for the code. well i know it would be a short s///, but was just looking up for if anyone has hook this on the network, so that i won't have to type. simply lazy.

    but really, it might stab me again if i'll have to remember escaping everytime there's a regexp, or everytime there's a possibility to have big5 input. that's why i tried to ask if this can be solved by locale, or something else, that i don't have to write..

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://181602]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (16)
As of 2016-07-27 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?


















    Results (245 votes). Check out past polls.