Home of the Squeezebox™ & Transporter® network music players.
Results 1 to 6 of 6
  1. #1
    Senior Member
    Join Date
    Apr 2005
    Location
    UK/London
    Posts
    3,015

    Perl regex and \Q\E

    Another thing I expect I knew long ago ... it just caught me out and resulted in incorrect URLs being generated.
    Without trying it .. what do you think will be printed for $x and $y ?

    Code:
    my $id = 'an-id';
    my $x = '(id)';
    
    $x =~ s/\(id\)/\Q$id\E/g;
    
    print "x=$x\n";
    
    my $y = '(id)';
    
    $y =~ s/\(id\)/$id/g;
    
    print "y=$y\n";
    Paul Webster
    http://dabdig.blogspot.com
    Author of "Now Playing" plugins covering Radio France (FIP etc), KCRW, Supla Finland, ABC Australia, CBC/Radio-Canada and RTE Ireland

  2. #2
    Babelfish's Best Boy mherger's Avatar
    Join Date
    Apr 2005
    Location
    Switzerland
    Posts
    20,460

    Perl regex and \Q\E

    > $x =~ s/\(id\)/\Q$id\E/g;

    Heh... I failed! And actually was surprised to see that these would work
    in the replacement expression...

    --

    Michael

  3. #3
    jvromans@squirrel.nl
    Guest

    Perl regex and \Q\E

    On Thu, 7 May 2020 12:01:58 +0200, Michael Herger <slim (AT) herger (DOT) net> wrote:

    > > $x =~ s/\(id\)/\Q$id\E/g;

    >
    > Heh... I failed! And actually was surprised to see that these would work
    > in the replacement expression...
    >


    Why surprised? \Q and \E are string meta characters and identical to
    quotemeta(...).

    The fact that they are mostly used in the match part of regexes doesn't
    mean they are not useful elsewhere .

    -- Johan

  4. #4
    Senior Member
    Join Date
    Apr 2005
    Location
    UK/London
    Posts
    3,015
    Quote Originally Posted by jvromans@squirrel.nl View Post
    Why surprised? \Q and \E are string meta characters and identical to
    quotemeta(...).
    So - what do you predict for the values of $x and $y ?
    Paul Webster
    http://dabdig.blogspot.com
    Author of "Now Playing" plugins covering Radio France (FIP etc), KCRW, Supla Finland, ABC Australia, CBC/Radio-Canada and RTE Ireland

  5. #5
    jvromans@squirrel.nl
    Guest

    Perl regex and \Q\E

    On Fri, 8 May 2020 05:28:49 +0000, Paul Webster
    <Paul.Webster.9ijhnz (AT) no-mx (DOT) forums.slimdevices.com> wrote:

    > jvromans (AT) squirrel (DOT) nl wrote:
    > >
    > > Why surprised? \Q and \E are string meta characters and identical to
    > > quotemeta(...).
    > >

    >
    > So - what do you predict for the values of $x and $y ?


    my $id = 'an-id';
    my $x = '(id)';

    $x =~ s/\(id\)/\Q$id\E/g;

    \(id\) matches completely $x, so its content is totally replaced by the
    replacement part. \Q$id\E is quotemeta($id) is an\-id ("an\\-id").

    my $y = '(id)';

    $y =~ s/\(id\)/$id/g;

    Again, $y is matched completely and replaced by $id, which has the value
    an-id ("an-id").

    You are probably confused by the fact that regexes have a number of
    \-sequences with special meanings. For example, \d matches digits. However,
    \Q and \E are not regex specials, they are string escapes just like \n.

    -- Johan

  6. #6
    Senior Member
    Join Date
    Apr 2005
    Location
    UK/London
    Posts
    3,015
    You are right - at least in the predicted outcome.
    My mistake was relying on faulty memory for the \Q\E actually did.

    I was using it because the source of the match was outside of my control (equivalent to user input).

    my objective was to turn something like
    /somewhere/onsite/{programme}
    to
    /somewhere/onsite/an-id
    but was getting
    /somewhere/onsite/an\-id

    I didn't want regex special characters in the input string (if there were any) to cause some sort of match
    e.g. if
    my $id = '(sometext)';
    then I wanted the result to be
    /somewhere/onsite/(sometext)
    (which would have been url-encoded later in the process)

    However, I was not thinking properly ... the \Q\E would have been relevant for external input (if there was any) on the search string not the replacement.

    (at least I hope that is right this time)
    Last edited by Paul Webster; 2020-05-08 at 00:20.
    Paul Webster
    http://dabdig.blogspot.com
    Author of "Now Playing" plugins covering Radio France (FIP etc), KCRW, Supla Finland, ABC Australia, CBC/Radio-Canada and RTE Ireland

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •