How to "quote" all regexp expression special characters?

D

david.karr

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?
 
B

Bart Van der Donck

david.karr said:
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;
 
D

david.karr

Bart said:
david.karr said:
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 ...
 
D

david.karr

Ah, after a little more digging, I noticed the "quotemeta" function.
This appears to do exactly what I want.

david.karr said:
Bart said:
david.karr said:
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 ...
 

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

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top