Problem with pattern match

Discussion in 'Perl Misc' started by neilsolent, Mar 8, 2007.

  1. neilsolent

    neilsolent Guest

    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
     
    neilsolent, Mar 8, 2007
    #1
    1. Advertising

  2. neilsolent

    -berlin.de Guest

    neilsolent <> 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
     
    -berlin.de, Mar 8, 2007
    #2
    1. Advertising

  3. 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
     
    Jürgen Exner, Mar 8, 2007
    #3
  4. neilsolent

    Mumia W. Guest

    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"
     
    Mumia W., Mar 8, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris

    Pattern match problem

    Chris, Jan 14, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    107
    Chris
    Jan 14, 2004
  2. Dafke8

    problem with pattern match

    Dafke8, Apr 30, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    93
    Dafke8
    May 1, 2004
  3. Lex

    pattern match problem

    Lex, Jun 17, 2004, in forum: Perl Misc
    Replies:
    8
    Views:
    132
  4. Niall Macpherson

    Problem with memory usage in pattern match

    Niall Macpherson, Dec 5, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    187
    Anno Siegel
    Dec 9, 2005
  5. samuel

    Multiple Line Pattern Match problem

    samuel, May 31, 2007, in forum: Perl Misc
    Replies:
    7
    Views:
    147
    samuel
    Jun 4, 2007
Loading...

Share This Page