Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to "quote" all regexp expression special characters?

Reply
Thread Tools

How to "quote" all regexp expression special characters?

 
 
david.karr
Guest
Posts: n/a
 
      06-06-2006
I have some code that expects to receive a regular expression to check
for. This part works fine. I'm now adding code that is trying to set
the regular expression to use, but such that any regular expression
special characters are "quoted" to be treated as regular characters.
What operator or function that takes a string will return the original
string, but with all regexp special characters quoted?

 
Reply With Quote
 
 
 
 
Bart Van der Donck
Guest
Posts: n/a
 
      06-06-2006
david.karr wrote:

> I have some code that expects to receive a regular expression to check
> for. This part works fine. I'm now adding code that is trying to set
> the regular expression to use, but such that any regular expression
> special characters are "quoted" to be treated as regular characters.
> What operator or function that takes a string will return the original
> string, but with all regexp special characters quoted?


#!perl
use strict; use warnings;
# See \Q on http://www.perl.com/doc/manual/html/pod/perlre.html
# I think the safest would be:
my $str = '.$*\n^?/+\"@&[0-5]';
my $search4 = '.$*\n^?/+\"@&[0-5]';
$str =~ s/\Q$search4/something/;
print $str;

--
Bart

 
Reply With Quote
 
 
 
 
david.karr
Guest
Posts: n/a
 
      06-06-2006

Bart Van der Donck wrote:
> david.karr wrote:
>
> > I have some code that expects to receive a regular expression to check
> > for. This part works fine. I'm now adding code that is trying to set
> > the regular expression to use, but such that any regular expression
> > special characters are "quoted" to be treated as regular characters.
> > What operator or function that takes a string will return the original
> > string, but with all regexp special characters quoted?

>
> #!perl
> use strict; use warnings;
> # See \Q on http://www.perl.com/doc/manual/html/pod/perlre.html
> # I think the safest would be:
> my $str = '.$*\n^?/+\"@&[0-5]';
> my $search4 = '.$*\n^?/+\"@&[0-5]';
> $str =~ s/\Q$search4/something/;
> print $str;


Ok, this looks close, but what I have to do is build into "$search4" a
string that is quoted, not to quote the "$search4" value. I tried
doing something like (the perl doc implies that "\Q" should be matched
with a "\E" for completeness):

$search4 = "\Q" . $stuff . "\E";

or

$search4 = "\\Q" . $stuff . "\\E";

Neither of these works. The first seems to completely ignore the "\Q"
and "\E", and the second causes warnings like this to be emitted:

Unrecognized escape \Q passed through in regex ...

 
Reply With Quote
 
david.karr
Guest
Posts: n/a
 
      06-06-2006
Ah, after a little more digging, I noticed the "quotemeta" function.
This appears to do exactly what I want.

david.karr wrote:
> Bart Van der Donck wrote:
> > david.karr wrote:
> >
> > > I have some code that expects to receive a regular expression to check
> > > for. This part works fine. I'm now adding code that is trying to set
> > > the regular expression to use, but such that any regular expression
> > > special characters are "quoted" to be treated as regular characters.
> > > What operator or function that takes a string will return the original
> > > string, but with all regexp special characters quoted?

> >
> > #!perl
> > use strict; use warnings;
> > # See \Q on http://www.perl.com/doc/manual/html/pod/perlre.html
> > # I think the safest would be:
> > my $str = '.$*\n^?/+\"@&[0-5]';
> > my $search4 = '.$*\n^?/+\"@&[0-5]';
> > $str =~ s/\Q$search4/something/;
> > print $str;

>
> Ok, this looks close, but what I have to do is build into "$search4" a
> string that is quoted, not to quote the "$search4" value. I tried
> doing something like (the perl doc implies that "\Q" should be matched
> with a "\E" for completeness):
>
> $search4 = "\Q" . $stuff . "\E";
>
> or
>
> $search4 = "\\Q" . $stuff . "\\E";
>
> Neither of these works. The first seems to completely ignore the "\Q"
> and "\E", and the second causes warnings like this to be emitted:
>
> Unrecognized escape \Q passed through in regex ...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
RegExp pattern to escape ALL special characters (but exclude unicodechars) Gabriela Javascript 4 12-22-2008 06:04 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM



Advertisments