Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem with pattern match

Reply
Thread Tools

Problem with pattern match

 
 
neilsolent
Guest
Posts: n/a
 
      03-08-2007
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

 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      03-08-2007
neilsolent <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      03-08-2007
neilsolent wrote:
> 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


 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      03-08-2007
On 03/08/2007 09:07 AM, neilsolent wrote:
> 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"


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Line Pattern Match problem samuel Perl Misc 7 06-04-2007 02:12 AM
Problem with memory usage in pattern match Niall Macpherson Perl Misc 2 12-09-2005 12:59 PM
pattern match problem Lex Perl Misc 8 06-18-2004 05:06 PM
problem with pattern match Dafke8 Perl Misc 6 05-01-2004 10:26 AM
Pattern match problem Chris Perl Misc 3 01-14-2004 08:26 PM



Advertisments