stop plus (+) symbol within scalar variable being interpolated in RE when followed by a number

T

Tom

Hi,

Can someone please tell me how to get round this problem. To simplify
it, I give a small example:

my $string = "catch+22";

my $RE = "h+22";

if ($string =~ /$RE/) {

print "I found the RE.\n";
}


It does not find the string h+22 within the catch+22. I can see why,
but don't know how to remedy it.

Thanks for the help.

Tom.
 
G

Gunnar Hjalmarsson

Tom said:
my $string = "catch+22";

my $RE = "h+22";

if ($string =~ /$RE/) {

print "I found the RE.\n";
}

It does not find the string h+22 within the catch+22. I can see
why, but don't know how to remedy it.

my $RE = quotemeta("h+22");

or

my $RE = "h\\+22";

or

my $RE = 'h\+22';

or

my $RE = qr'h\+22';

or

if ($string =~ /\Q$RE/) {

or some other way...
 
M

Matt Garrish

Tom said:
Hi,

Can someone please tell me how to get round this problem. To simplify
it, I give a small example:

my $string = "catch+22";

my $RE = "h+22";

if ($string =~ /$RE/) {

if ($string =~ /\Q$RE/)

You need to escape any metacharacters within the string with \Q. The \E
would be a bit pointless in your example, so I've omitted it, but if you
didn't want to escape any metacharacters after $RE you could end the quoting
that way (i.e., \Q ... \E). Read over the perlre man page for more
information.

Matt
 
M

Matthew Braid

Tom said:
Hi,

Can someone please tell me how to get round this problem. To simplify
it, I give a small example:

my $string = "catch+22";

my $RE = "h+22";

if ($string =~ /$RE/) {

print "I found the RE.\n";
}


It does not find the string h+22 within the catch+22. I can see why,
but don't know how to remedy it.

Thanks for the help.

Tom.

if ($string =~ /\Q$RE/) {
print "I found the RE!\n";
}

\Q quotes everything up to the end of the re or until a \E is found.

There is still a problem - if $RE contains '\E'. I don't know how to get
around that without explicitly searching for it in the variable and
quoting it again yourself (but I think its fixed in perl 6 since it uses
something like \Q{$RE})

MB
 
B

Bart Lateur

Matthew said:
if ($string =~ /\Q$RE/) {
print "I found the RE!\n";
}

\Q quotes everything up to the end of the re or until a \E is found.

There is still a problem - if $RE contains '\E'.

That's not how it works. \Q applies a quotemeta() to the whole of the
variables in the regex, irrespective of their contents. Only literal
embedded '\E' sequences in the regex itself, are taken into account.

$RE = 'a\E+';
$_ = 'aaaaa\E+++++';
/(\Q$RE\E)/ and print $1;
-->
a\E+
 
A

Anton Shcherbinin

my $string = "catch+22";
my $RE = "h+22";
if ($string =~ /$RE/) {
print "I found the RE.\n";
}
It does not find the string h+22 within the catch+22. I can see why,
but don't know how to remedy it.

Tom, it looks like you just need to find a substring $RE within a string
$string. If so, you may not use regular expressions at all.

print "I found the RE.\n" if index($string, $RE) > -1;

index() function will do.
 
T

Tom

Thanks for all your help everyone, much appreciated. It solves my
bigger problem perfectly.
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top