suppress regex parsing in interpolated string

Discussion in 'Perl Misc' started by stevenkobes@hotmail.com, Feb 28, 2006.

  1. Guest

    I have
    s/$foo/$bar/;

    But $foo and $bar might have funny characters [ ] { } etc., and I want
    them taken literally rather than parsed as regular expressions.

    How can I do this substitution? Thanks.
     
    , Feb 28, 2006
    #1
    1. Advertising

  2. wrote:
    > I have
    > s/$foo/$bar/;
    >
    > But $foo and $bar might have funny characters [ ] { } etc., and I want
    > them taken literally rather than parsed as regular expressions.
    >
    > How can I do this substitution? Thanks.


    s/\Q$foo\E/$bar/;

    ....the REPLACEMENT portion is not a regular expression, so doesn't need
    to be escaped--unless you're talking about URI escapes, or something
    like that, in which case that can be done before the s/// line.
     
    it_says_BALLS_on_your forehead, Feb 28, 2006
    #2
    1. Advertising

  3. DJ Stunks Guest

    wrote:
    > I have
    > s/$foo/$bar/;
    >
    > But $foo and $bar might have funny characters [ ] { } etc., and I want
    > them taken literally rather than parsed as regular expressions.
    >
    > How can I do this substitution? Thanks.


    perldoc -f quotemeta

    -jp
     
    DJ Stunks, Feb 28, 2006
    #3
  4. robic0 Guest

    On 28 Feb 2006 11:13:56 -0800, "it_says_BALLS_on_your forehead" <> wrote:

    >
    > wrote:
    >> I have
    >> s/$foo/$bar/;
    >>
    >> But $foo and $bar might have funny characters [ ] { } etc., and I want
    >> them taken literally rather than parsed as regular expressions.
    >>
    >> How can I do this substitution? Thanks.

    >
    >s/\Q$foo\E/$bar/;
    >

    Unfortunately, \Q will not do some regex esc characters. If $foo is dynamic
    and unknown, then that is a problem. I have a simple sub that does it all.
    I don't know why Perl doesn't do all of them, have to ask the designers.

    >...the REPLACEMENT portion is not a regular expression, so doesn't need
    >to be escaped--unless you're talking about URI escapes, or something
    >like that, in which case that can be done before the s/// line.
     
    robic0, Mar 1, 2006
    #4
  5. Paul Lalli Guest

    robic0 wrote:
    > On 28 Feb 2006 11:13:56 -0800, "it_says_BALLS_on_your forehead" <> wrote:
    >
    > >
    > >s/\Q$foo\E/$bar/;
    > >

    > Unfortunately, \Q will not do some regex esc characters.


    I don't suppose you have any code to back up this claim?

    > If $foo is dynamic
    > and unknown, then that is a problem.


    $foo is always dynamic. That's the point of a variable. Things that
    aren't dynamic are known as "constants" and "literals".

    What are you talking about?

    > I have a simple sub that does it all.


    Does what all? Once again, how about some code to back up your claims?

    Paul Lalli
     
    Paul Lalli, Mar 1, 2006
    #5
  6. robic0 Guest

    On Tue, 28 Feb 2006 16:54:52 -0800, robic0 wrote:

    >On 28 Feb 2006 11:13:56 -0800, "it_says_BALLS_on_your forehead" <> wrote:
    >
    >>
    >> wrote:
    >>> I have
    >>> s/$foo/$bar/;
    >>>
    >>> But $foo and $bar might have funny characters [ ] { } etc., and I want
    >>> them taken literally rather than parsed as regular expressions.
    >>>
    >>> How can I do this substitution? Thanks.

    >>
    >>s/\Q$foo\E/$bar/;
    >>

    >Unfortunately, \Q will not do some regex esc characters. If $foo is dynamic
    >and unknown, then that is a problem. I have a simple sub that does it all.
    >I don't know why Perl doesn't do all of them, have to ask the designers.
    >

    This is meant to the OP (not "it_says_BALLS_on_your forehead"):

    To followup (I should have included everything).
    The basic problem on the left (regex) side is that either its dynamic
    or static. If its static, you can intermix escaped escape codes with escape
    codes. If its dynamic, that is not possible.

    The *only* reason to have dynamic regex is to compare a source with a target
    within a loop. Ie: the regex is the last source (for example). In this case
    \Q won't cut it (see the docs).

    Try this to convert patterns (pattern side of regex, not replacement. ie: m/$pattern/$replacement/):

    sub convertPatternMeta
    {
    my ($pattern) = shift;
    my @regx_esc_codes =
    (
    "\\", '/', '(', ')', '[', ']', '?', '|',
    '+', '.', '*', '$', '^', '{', '}', '@'
    );
    foreach my $tc (@regx_esc_codes) {
    # code template for regex
    my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";
    eval $xxx;
    if ($@) {
    # the compiler will show the escape char, add
    # it char to @regx_esc_codes
    $@ =~ s/^[\s]+//s; $@ =~ s/[\s]+$//s;
    die "$@";
    }
    }
    return $pattern;
    }
     
    robic0, Mar 1, 2006
    #6
  7. robic0 Guest

    On 28 Feb 2006 17:02:39 -0800, "Paul Lalli" <> wrote:

    >robic0 wrote:
    >> On 28 Feb 2006 11:13:56 -0800, "it_says_BALLS_on_your forehead" <> wrote:
    >>
    >> >
    >> >s/\Q$foo\E/$bar/;
    >> >

    >> Unfortunately, \Q will not do some regex esc characters.

    >
    >I don't suppose you have any code to back up this claim?
    >

    I'd post it here but do your own reading...
    >> If $foo is dynamic
    >> and unknown, then that is a problem.

    >
    >$foo is always dynamic. That's the point of a variable. Things that

    ever heard of
    my $foo = qr/asdfasdfasdf/;
    while ($var =~ s/$foo/$bar/g) {}
    ?
    >aren't dynamic are known as "constants" and "literals".
    >
    >What are you talking about?

    What drugs are u on?
    >
    >> I have a simple sub that does it all.

    >
    >Does what all? Once again, how about some code to back up your claims?

    See the posts and get professional help, please..
    >
    >Paul Lalli
     
    robic0, Mar 1, 2006
    #7
  8. robic0 Guest

    On Tue, 28 Feb 2006 17:13:20 -0800, robic0 wrote:

    >On Tue, 28 Feb 2006 16:54:52 -0800, robic0 wrote:
    >
    >>On 28 Feb 2006 11:13:56 -0800, "it_says_BALLS_on_your forehead" <> wrote:
    >>
    >>>
    >>> wrote:
    >>>> I have
    >>>> s/$foo/$bar/;
    >>>>
    >>>> But $foo and $bar might have funny characters [ ] { } etc., and I want
    >>>> them taken literally rather than parsed as regular expressions.
    >>>>
    >>>> How can I do this substitution? Thanks.
    >>>
    >>>s/\Q$foo\E/$bar/;
    >>>

    >>Unfortunately, \Q will not do some regex esc characters. If $foo is dynamic
    >>and unknown, then that is a problem. I have a simple sub that does it all.
    >>I don't know why Perl doesn't do all of them, have to ask the designers.
    >>

    >This is meant to the OP (not "it_says_BALLS_on_your forehead"):
    >
    >To followup (I should have included everything).
    >The basic problem on the left (regex) side is that either its dynamic
    >or static. If its static, you can intermix escaped escape codes with escape
    >codes. If its dynamic, that is not possible.
    >
    >The *only* reason to have dynamic regex is to compare a source with a target
    >within a loop. Ie: the regex is the last source (for example). In this case
    >\Q won't cut it (see the docs).
    >
    >Try this to convert patterns (pattern side of regex, not replacement. ie: m/$pattern/$replacement/):
    >
    >sub convertPatternMeta
    >{
    > my ($pattern) = shift;
    > my @regx_esc_codes =
    > (
    > "\\", '/', '(', ')', '[', ']', '?', '|',
    > '+', '.', '*', '$', '^', '{', '}', '@'
    > );
    > foreach my $tc (@regx_esc_codes) {
    > # code template for regex
    > my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";
    > eval $xxx;
    > if ($@) {
    > # the compiler will show the escape char, add
    > # it char to @regx_esc_codes
    > $@ =~ s/^[\s]+//s; $@ =~ s/[\s]+$//s;

    followup, this is old code, before the hacks call me on this (its actually not an error):
    $@ =~ s/^\s+//s; $@ =~ s/\s+$//s;
    but the whole line isint necessesary (chopped code of chopped code, hundred thousand lines)
    > die "$@";
    > }
    > }
    > return $pattern;
    >}
    >
     
    robic0, Mar 1, 2006
    #8
  9. Uri Guttman Guest

    >>>>> "r" == robic0 <robic0> writes:

    r> On 28 Feb 2006 17:02:39 -0800, "Paul Lalli" <> wrote:
    >> robic0 wrote:
    >>
    >> I don't suppose you have any code to back up this claim?
    >>
    >> $foo is always dynamic. That's the point of a variable. Things that

    r> ever heard of
    r> my $foo = qr/asdfasdfasdf/;
    r> while ($var =~ s/$foo/$bar/g) {}

    and show the input and output and exactly what regex chars are not
    escaped. that code doesn't do anything so it is not a proof of your
    wrong contention.

    r> See the posts and get professional help, please..

    look in the mirror if it isn't cracked already.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Mar 1, 2006
    #9
  10. robic0 <> wrote:

    > my @regx_esc_codes =
    > (
    > "\\", '/', '(', ')', '[', ']', '?', '|',
    > '+', '.', '*', '$', '^', '{', '}', '@'
    > );



    Neither '/' nor '@' are regex metacharacters, and
    therefore need no escaping.


    ---------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    $_ = 'home directory for is /home/robic0/';

    my $dir_pattern = '/home/robic0/';
    print "matched directory\n" if /$dir_pattern/; # matches just fine

    my $email_pattern = '';
    print "matched email\n" if /$email_pattern/; # look Ma! no backslashes!
    ---------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 1, 2006
    #10
  11. Dr.Ruud Guest

    Tad McClellan schreef:



    > $_ = 'home directory for is /home/robic0/';
    > [...]
    > my $email_pattern = '';
    > print "matched email\n" if /$email_pattern/; # look Ma! no
    > backslashes!


    This should also be matched:
    "home directory for robic0@yahoo\x{066D}com is /home/robic0/"

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Mar 1, 2006
    #11
  12. robic0 Guest

    On Wed, 1 Mar 2006 07:22:15 -0600, Tad McClellan <> wrote:

    >robic0 <> wrote:
    >
    >> my @regx_esc_codes =
    >> (
    >> "\\", '/', '(', ')', '[', ']', '?', '|',
    >> '+', '.', '*', '$', '^', '{', '}', '@'
    >> );

    >
    >
    >Neither '/' nor '@' are regex metacharacters, and
    >therefore need no escaping.
    >
    >
    >---------------------
    >#!/usr/bin/perl
    >use warnings;
    >use strict;
    >
    >$_ = 'home directory for is /home/robic0/';
    >
    >my $dir_pattern = '/home/robic0/';
    >print "matched directory\n" if /$dir_pattern/; # matches just fine
    >
    >my $email_pattern = '';
    >print "matched email\n" if /$email_pattern/; # look Ma! no backslashes!
    >---------------------


    Yeah your probably right Tad. But I wrote it 2 years ago when I was 2 months
    into Perl.

    Hey, I wonder if you can tell me what exactly this line does (minus esc for eval)
    and why it has to be this way (or does it? hehe) -

    my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";
     
    robic0, Mar 9, 2006
    #12
  13. robic0 Guest

    On Wed, 1 Mar 2006 18:18:58 +0100, "Dr.Ruud" <> wrote:

    >Tad McClellan schreef:
    >
    >
    >
    >> $_ = 'home directory for is /home/robic0/';
    >> [...]
    >> my $email_pattern = '';
    >> print "matched email\n" if /$email_pattern/; # look Ma! no
    >> backslashes!

    >
    >This should also be matched:
    >"home directory for robic0@yahoo\x{066D}com is /home/robic0/"


    Here's one for you Dr. Ruud

    Tell me what exactly this line does (minus esc for eval)
    and why it has to be this way (or does it? hehe) -

    my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";
     
    robic0, Mar 9, 2006
    #13
  14. robic0 Guest

    On Wed, 1 Mar 2006 18:18:58 +0100, "Dr.Ruud" <> wrote:

    >Tad McClellan schreef:
    >
    >
    >
    >> $_ = 'home directory for is /home/robic0/';
    >> [...]
    >> my $email_pattern = '';
    >> print "matched email\n" if /$email_pattern/; # look Ma! no
    >> backslashes!

    >
    >This should also be matched:
    >"home directory for robic0@yahoo\x{066D}com is /home/robic0/"


    This "{066D}" is not matched?
     
    robic0, Mar 9, 2006
    #14
  15. robic0 Guest

    On Wed, 1 Mar 2006 07:22:15 -0600, Tad McClellan <> wrote:

    >robic0 <> wrote:
    >
    >> my @regx_esc_codes =
    >> (
    >> "\\", '/', '(', ')', '[', ']', '?', '|',
    >> '+', '.', '*', '$', '^', '{', '}', '@'
    >> );

    >
    >
    >Neither '/' nor '@' are regex metacharacters, and
    >therefore need no escaping.
    >
    >
    >---------------------
    >#!/usr/bin/perl
    >use warnings;
    >use strict;
    >
    >$_ = 'home directory for is /home/robic0/';
    >
    >my $dir_pattern = '/home/robic0/';
    >print "matched directory\n" if /$dir_pattern/; # matches just fine
    >
    >my $email_pattern = '';
    >print "matched email\n" if /$email_pattern/; # look Ma! no backslashes!
    >---------------------


    Actually, there is no reason not to escape every printable character,
    doesen't matter if its a metacharacter.
     
    robic0, Mar 9, 2006
    #15
  16. robic0 wrote:

    > Hey, I wonder if you can tell me what exactly this line does (minus esc
    > for eval) and why it has to be this way (or does it? hehe) -
    >
    > my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";


    It assigns a string to $xxx. What else should it do?
    --
    josef punkt moellers bei gmx punkt de
     
    Josef Möllers, Mar 9, 2006
    #16
  17. robic0 Guest

    On Thu, 09 Mar 2006 08:29:39 +0100, Josef Möllers <> wrote:

    >robic0 wrote:
    >
    >> Hey, I wonder if you can tell me what exactly this line does (minus esc
    >> for eval) and why it has to be this way (or does it? hehe) -
    >>
    >> my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";

    ^^^^^^
    these
    >
    >It assigns a string to $xxx. What else should it do?
     
    robic0, Mar 10, 2006
    #17
    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. Bjorn Sagbakken

    Ajax animation: Interpolated, question

    Bjorn Sagbakken, Nov 18, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    413
    Bjorn Sagbakken
    Nov 18, 2007
  2. Richard Kilmer
    Replies:
    12
    Views:
    233
    Richard Kilmer
    Oct 5, 2004
  3. Replies:
    9
    Views:
    117
  4. Tom
    Replies:
    6
    Views:
    131
  5. Replies:
    6
    Views:
    135
Loading...

Share This Page