$string =~ /$pattern/i

C

cracraft

Hi,

I am attempting to find out if a $string contains $pattern.

$string =~ /$pattern/i

normally solves it.

However, I noticed that if $pattern contains something like

(4)

and $string contains something like

ABC(4)

then

$string =~ /$pattern/i

does not work, because of the parenthesis.

I want a flat-out literal match where $string can contain any
occurrence of $pattern regardless of parenthesis.

In other words, I don't want the special characters of regular
expressions interpreted.

I could write my own pattern matcher but resist it and would
think Perl could provide the above feature.

Anybody?

--Stuart
 
G

Gunnar Hjalmarsson

I am attempting to find out if a $string contains $pattern.

$string =~ /$pattern/i

normally solves it.

However, I noticed that if $pattern contains something like

(4)

and $string contains something like

ABC(4)

then

$string =~ /$pattern/i

does not work, because of the parenthesis.

I want a flat-out literal match where $string can contain any
occurrence of $pattern regardless of parenthesis.

In other words, I don't want the special characters of regular
expressions interpreted.

$string =~ /\Q$pattern/i
I could write my own pattern matcher

Really? And still not able to solve this problem?
 
P

Paul Lalli

I want a flat-out literal match where $string can contain any
occurrence of $pattern regardless of parenthesis.

perldoc -f index
In other words, I don't want the special characters of regular
expressions interpreted.

perldoc -f quotemeta


Paul Lalli
 
X

xhoster

Tad McClellan said:
Yes it does.

I tried to weed my garden with it, and I'm not getting anywhere.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
P

Peter J. Holzer

I don't know what version of Perl you use but in my Perl 5.6.1 quotemeta
sometime return curious results ;-)

print quotemeta('This is a string');

By me the space (0x20h) is not need to escape.

The space must be escaped if used in a regexp with the /x modifier.
And escaping doesn't hurt otherwise, so this is not an example where
quotemeta "doesn't work".

hp
 
T

Tad McClellan

Petr Vileta said:
I don't know what version of Perl you use


That's OK, as it is not relevant.

but in my Perl 5.6.1 quotemeta
sometime return curious results ;-)

print quotemeta('This is a string');

By me the space (0x20h) is not need to escape.


Errr, then don't call quotemeta on it!
 
T

Tad McClellan

Petr Vileta said:
Tad said:
Errr, then don't call quotemeta on it!

This was be an example. In real script I mean something like
my $variable = myfunction(quotemeta($ARGV[0]));

where myfunction() need to get escaped string as parameter but escaped space
is nonsese.
Now I realise it as
my $arg = quotemeta($ARGV[0]);
$arg =~ s/\\\x20/ /g;
my $variable = myfunction($arg);

Know you better way?


Sure. The first sentence of its documentation says:

Returns the value of EXPR with all non-"word" characters backslashed.

If you want all non-"word" characters except space backslashed,
then write a s/// that does that instead:

$arg =~ s/([^\w ])/\\$1/g;
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top