Problem with pattern match

N

neilsolent

Hi

I need to do a pattern match like:

$a =~ /(.*)\/$pattern/;

Trouble is, the variable $pattern contains a string of weird and
wonderful characters like "+" which seem to get treated as special in
the regular expression, so I get an error. Is there some way I can
match using $pattern literally (i.e. ignore the special meaning of any
characters)? Needless to day, $pattern is not predetermined, so I
cannot simpy type out a valid regular expression manually.

many thanks,
Neil
 
A

anno4000

neilsolent said:
Hi

I need to do a pattern match like:

$a =~ /(.*)\/$pattern/;

Trouble is, the variable $pattern contains a string of weird and
wonderful characters like "+" which seem to get treated as special in
the regular expression, so I get an error. Is there some way I can
match using $pattern literally (i.e. ignore the special meaning of any
characters)? Needless to day, $pattern is not predetermined, so I
cannot simpy type out a valid regular expression manually.

m{(.*)/\Q$pattern};

See perlre for the meaning of "\Q", and its companion "\E". Using {}
as regex delimiters allows you to specify "/" without escaping it.

The name $pattern is probably a bit misleading. If it were a
(partial) pattern you'd *want* the special characters interpreted
as usual in a regex. The way you have it, it is rather a string to
be matched literally.

Anno
 
J

Jürgen Exner

neilsolent said:
I need to do a pattern match like:

$a =~ /(.*)\/$pattern/;

Trouble is, the variable $pattern contains a string of weird and
wonderful characters like "+" which seem to get treated as special in
the regular expression, so I get an error. Is there some way I can
match using $pattern literally (i.e. ignore the special meaning of any
characters)?

It seems to me that you want to check if $pattern is a literal substring of
$a, not a RE pattern. If so then why are you cranking the big RE engine
instead of simply checking if it is a substring?

perldoc -f index

jue
 
M

Mumia W.

Hi

I need to do a pattern match like:

$a =~ /(.*)\/$pattern/;

Trouble is, the variable $pattern contains a string of weird and
wonderful characters like "+" which seem to get treated as special in
the regular expression, so I get an error. Is there some way I can
match using $pattern literally (i.e. ignore the special meaning of any
characters)? Needless to day, $pattern is not predetermined, so I
cannot simpy type out a valid regular expression manually.

many thanks,
Neil

$a =~ /(.*)\/\Q$pattern\E/;

Read about the quotemeta function: "perldoc -f quotemeta"
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top