suppress regex parsing in interpolated string

S

stevenkobes

I have
s/$foo/$bar/;

But $foo and $bar might have funny characters [ ] { } etc., and I want
them taken literally rather than parsed as regular expressions.

How can I do this substitution? Thanks.
 
I

it_says_BALLS_on_your forehead

I have
s/$foo/$bar/;

But $foo and $bar might have funny characters [ ] { } etc., and I want
them taken literally rather than parsed as regular expressions.

How can I do this substitution? Thanks.

s/\Q$foo\E/$bar/;

....the REPLACEMENT portion is not a regular expression, so doesn't need
to be escaped--unless you're talking about URI escapes, or something
like that, in which case that can be done before the s/// line.
 
D

DJ Stunks

I have
s/$foo/$bar/;

But $foo and $bar might have funny characters [ ] { } etc., and I want
them taken literally rather than parsed as regular expressions.

How can I do this substitution? Thanks.

perldoc -f quotemeta

-jp
 
R

robic0

I have
s/$foo/$bar/;

But $foo and $bar might have funny characters [ ] { } etc., and I want
them taken literally rather than parsed as regular expressions.

How can I do this substitution? Thanks.

s/\Q$foo\E/$bar/;
Unfortunately, \Q will not do some regex esc characters. If $foo is dynamic
and unknown, then that is a problem. I have a simple sub that does it all.
I don't know why Perl doesn't do all of them, have to ask the designers.
 
P

Paul Lalli

robic0 said:
Unfortunately, \Q will not do some regex esc characters.

I don't suppose you have any code to back up this claim?
If $foo is dynamic
and unknown, then that is a problem.

$foo is always dynamic. That's the point of a variable. Things that
aren't dynamic are known as "constants" and "literals".

What are you talking about?
I have a simple sub that does it all.

Does what all? Once again, how about some code to back up your claims?

Paul Lalli
 
R

robic0

I have
s/$foo/$bar/;

But $foo and $bar might have funny characters [ ] { } etc., and I want
them taken literally rather than parsed as regular expressions.

How can I do this substitution? Thanks.

s/\Q$foo\E/$bar/;
Unfortunately, \Q will not do some regex esc characters. If $foo is dynamic
and unknown, then that is a problem. I have a simple sub that does it all.
I don't know why Perl doesn't do all of them, have to ask the designers.
This is meant to the OP (not "it_says_BALLS_on_your forehead"):

To followup (I should have included everything).
The basic problem on the left (regex) side is that either its dynamic
or static. If its static, you can intermix escaped escape codes with escape
codes. If its dynamic, that is not possible.

The *only* reason to have dynamic regex is to compare a source with a target
within a loop. Ie: the regex is the last source (for example). In this case
\Q won't cut it (see the docs).

Try this to convert patterns (pattern side of regex, not replacement. ie: m/$pattern/$replacement/):

sub convertPatternMeta
{
my ($pattern) = shift;
my @regx_esc_codes =
(
"\\", '/', '(', ')', '[', ']', '?', '|',
'+', '.', '*', '$', '^', '{', '}', '@'
);
foreach my $tc (@regx_esc_codes) {
# code template for regex
my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";
eval $xxx;
if ($@) {
# the compiler will show the escape char, add
# it char to @regx_esc_codes
$@ =~ s/^[\s]+//s; $@ =~ s/[\s]+$//s;
die "$@";
}
}
return $pattern;
}
 
R

robic0

I don't suppose you have any code to back up this claim?
I'd post it here but do your own reading...
$foo is always dynamic. That's the point of a variable. Things that
ever heard of
my $foo = qr/asdfasdfasdf/;
while ($var =~ s/$foo/$bar/g) {}
?
aren't dynamic are known as "constants" and "literals".

What are you talking about? What drugs are u on?


Does what all? Once again, how about some code to back up your claims?
See the posts and get professional help, please..
 
R

robic0

(e-mail address removed) wrote:
I have
s/$foo/$bar/;

But $foo and $bar might have funny characters [ ] { } etc., and I want
them taken literally rather than parsed as regular expressions.

How can I do this substitution? Thanks.

s/\Q$foo\E/$bar/;
Unfortunately, \Q will not do some regex esc characters. If $foo is dynamic
and unknown, then that is a problem. I have a simple sub that does it all.
I don't know why Perl doesn't do all of them, have to ask the designers.
This is meant to the OP (not "it_says_BALLS_on_your forehead"):

To followup (I should have included everything).
The basic problem on the left (regex) side is that either its dynamic
or static. If its static, you can intermix escaped escape codes with escape
codes. If its dynamic, that is not possible.

The *only* reason to have dynamic regex is to compare a source with a target
within a loop. Ie: the regex is the last source (for example). In this case
\Q won't cut it (see the docs).

Try this to convert patterns (pattern side of regex, not replacement. ie: m/$pattern/$replacement/):

sub convertPatternMeta
{
my ($pattern) = shift;
my @regx_esc_codes =
(
"\\", '/', '(', ')', '[', ']', '?', '|',
'+', '.', '*', '$', '^', '{', '}', '@'
);
foreach my $tc (@regx_esc_codes) {
# code template for regex
my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";
eval $xxx;
if ($@) {
# the compiler will show the escape char, add
# it char to @regx_esc_codes
$@ =~ s/^[\s]+//s; $@ =~ s/[\s]+$//s;
followup, this is old code, before the hacks call me on this (its actually not an error):
$@ =~ s/^\s+//s; $@ =~ s/\s+$//s;
but the whole line isint necessesary (chopped code of chopped code, hundred thousand lines)
 
U

Uri Guttman

"r" == robic0 <robic0> writes:

r> ever heard of
r> my $foo = qr/asdfasdfasdf/;
r> while ($var =~ s/$foo/$bar/g) {}

and show the input and output and exactly what regex chars are not
escaped. that code doesn't do anything so it is not a proof of your
wrong contention.

r> See the posts and get professional help, please..

look in the mirror if it isn't cracked already.

uri
 
T

Tad McClellan

robic0 said:
my @regx_esc_codes =
(
"\\", '/', '(', ')', '[', ']', '?', '|',
'+', '.', '*', '$', '^', '{', '}', '@'
);


Neither '/' nor '@' are regex metacharacters, and
therefore need no escaping.


---------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = 'home directory for (e-mail address removed) is /home/robic0/';

my $dir_pattern = '/home/robic0/';
print "matched directory\n" if /$dir_pattern/; # matches just fine

my $email_pattern = '(e-mail address removed)';
print "matched email\n" if /$email_pattern/; # look Ma! no backslashes!
 
D

Dr.Ruud

Tad McClellan schreef:


$_ = 'home directory for (e-mail address removed) is /home/robic0/';
[...]
my $email_pattern = '(e-mail address removed)';
print "matched email\n" if /$email_pattern/; # look Ma! no
backslashes!

This should also be matched:
"home directory for robic0@yahoo\x{066D}com is /home/robic0/"
 
R

robic0

robic0 said:
my @regx_esc_codes =
(
"\\", '/', '(', ')', '[', ']', '?', '|',
'+', '.', '*', '$', '^', '{', '}', '@'
);


Neither '/' nor '@' are regex metacharacters, and
therefore need no escaping.


---------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = 'home directory for (e-mail address removed) is /home/robic0/';

my $dir_pattern = '/home/robic0/';
print "matched directory\n" if /$dir_pattern/; # matches just fine

my $email_pattern = '(e-mail address removed)';
print "matched email\n" if /$email_pattern/; # look Ma! no backslashes!
---------------------

Yeah your probably right Tad. But I wrote it 2 years ago when I was 2 months
into Perl.

Hey, I wonder if you can tell me what exactly this line does (minus esc for eval)
and why it has to be this way (or does it? hehe) -

my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";
 
R

robic0

Tad McClellan schreef:


$_ = 'home directory for (e-mail address removed) is /home/robic0/';
[...]
my $email_pattern = '(e-mail address removed)';
print "matched email\n" if /$email_pattern/; # look Ma! no
backslashes!

This should also be matched:
"home directory for robic0@yahoo\x{066D}com is /home/robic0/"

Here's one for you Dr. Ruud

Tell me what exactly this line does (minus esc for eval)
and why it has to be this way (or does it? hehe) -

my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";
 
R

robic0

Tad McClellan schreef:


$_ = 'home directory for (e-mail address removed) is /home/robic0/';
[...]
my $email_pattern = '(e-mail address removed)';
print "matched email\n" if /$email_pattern/; # look Ma! no
backslashes!

This should also be matched:
"home directory for robic0@yahoo\x{066D}com is /home/robic0/"

This "{066D}" is not matched?
 
R

robic0

robic0 said:
my @regx_esc_codes =
(
"\\", '/', '(', ')', '[', ']', '?', '|',
'+', '.', '*', '$', '^', '{', '}', '@'
);


Neither '/' nor '@' are regex metacharacters, and
therefore need no escaping.


---------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = 'home directory for (e-mail address removed) is /home/robic0/';

my $dir_pattern = '/home/robic0/';
print "matched directory\n" if /$dir_pattern/; # matches just fine

my $email_pattern = '(e-mail address removed)';
print "matched email\n" if /$email_pattern/; # look Ma! no backslashes!
---------------------

Actually, there is no reason not to escape every printable character,
doesen't matter if its a metacharacter.
 
J

Josef Möllers

robic0 said:
Hey, I wonder if you can tell me what exactly this line does (minus esc
for eval) and why it has to be this way (or does it? hehe) -

my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";

It assigns a string to $xxx. What else should it do?
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top