How to "quote" all regexp expression special characters?

Discussion in 'Perl Misc' started by david.karr, Jun 6, 2006.

  1. david.karr

    david.karr Guest

    I have some code that expects to receive a regular expression to check
    for. This part works fine. I'm now adding code that is trying to set
    the regular expression to use, but such that any regular expression
    special characters are "quoted" to be treated as regular characters.
    What operator or function that takes a string will return the original
    string, but with all regexp special characters quoted?
     
    david.karr, Jun 6, 2006
    #1
    1. Advertising

  2. david.karr wrote:

    > I have some code that expects to receive a regular expression to check
    > for. This part works fine. I'm now adding code that is trying to set
    > the regular expression to use, but such that any regular expression
    > special characters are "quoted" to be treated as regular characters.
    > What operator or function that takes a string will return the original
    > string, but with all regexp special characters quoted?


    #!perl
    use strict; use warnings;
    # See \Q on http://www.perl.com/doc/manual/html/pod/perlre.html
    # I think the safest would be:
    my $str = '.$*\n^?/+\"@&[0-5]';
    my $search4 = '.$*\n^?/+\"@&[0-5]';
    $str =~ s/\Q$search4/something/;
    print $str;

    --
    Bart
     
    Bart Van der Donck, Jun 6, 2006
    #2
    1. Advertising

  3. david.karr

    david.karr Guest

    Bart Van der Donck wrote:
    > david.karr wrote:
    >
    > > I have some code that expects to receive a regular expression to check
    > > for. This part works fine. I'm now adding code that is trying to set
    > > the regular expression to use, but such that any regular expression
    > > special characters are "quoted" to be treated as regular characters.
    > > What operator or function that takes a string will return the original
    > > string, but with all regexp special characters quoted?

    >
    > #!perl
    > use strict; use warnings;
    > # See \Q on http://www.perl.com/doc/manual/html/pod/perlre.html
    > # I think the safest would be:
    > my $str = '.$*\n^?/+\"@&[0-5]';
    > my $search4 = '.$*\n^?/+\"@&[0-5]';
    > $str =~ s/\Q$search4/something/;
    > print $str;


    Ok, this looks close, but what I have to do is build into "$search4" a
    string that is quoted, not to quote the "$search4" value. I tried
    doing something like (the perl doc implies that "\Q" should be matched
    with a "\E" for completeness):

    $search4 = "\Q" . $stuff . "\E";

    or

    $search4 = "\\Q" . $stuff . "\\E";

    Neither of these works. The first seems to completely ignore the "\Q"
    and "\E", and the second causes warnings like this to be emitted:

    Unrecognized escape \Q passed through in regex ...
     
    david.karr, Jun 6, 2006
    #3
  4. david.karr

    david.karr Guest

    Ah, after a little more digging, I noticed the "quotemeta" function.
    This appears to do exactly what I want.

    david.karr wrote:
    > Bart Van der Donck wrote:
    > > david.karr wrote:
    > >
    > > > I have some code that expects to receive a regular expression to check
    > > > for. This part works fine. I'm now adding code that is trying to set
    > > > the regular expression to use, but such that any regular expression
    > > > special characters are "quoted" to be treated as regular characters.
    > > > What operator or function that takes a string will return the original
    > > > string, but with all regexp special characters quoted?

    > >
    > > #!perl
    > > use strict; use warnings;
    > > # See \Q on http://www.perl.com/doc/manual/html/pod/perlre.html
    > > # I think the safest would be:
    > > my $str = '.$*\n^?/+\"@&[0-5]';
    > > my $search4 = '.$*\n^?/+\"@&[0-5]';
    > > $str =~ s/\Q$search4/something/;
    > > print $str;

    >
    > Ok, this looks close, but what I have to do is build into "$search4" a
    > string that is quoted, not to quote the "$search4" value. I tried
    > doing something like (the perl doc implies that "\Q" should be matched
    > with a "\E" for completeness):
    >
    > $search4 = "\Q" . $stuff . "\E";
    >
    > or
    >
    > $search4 = "\\Q" . $stuff . "\\E";
    >
    > Neither of these works. The first seems to completely ignore the "\Q"
    > and "\E", and the second causes warnings like this to be emitted:
    >
    > Unrecognized escape \Q passed through in regex ...
     
    david.karr, Jun 6, 2006
    #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. Replies:
    3
    Views:
    13,261
  2. Stimp
    Replies:
    2
    Views:
    2,279
    Stimp
    Sep 20, 2006
  3. Eric Layman
    Replies:
    3
    Views:
    635
    Rad [Visual C# MVP]
    Apr 14, 2007
  4. Replies:
    6
    Views:
    442
    Stefan Ram
    Jun 15, 2008
  5. Gabriela
    Replies:
    4
    Views:
    548
    Tim Greer
    Dec 22, 2008
Loading...

Share This Page