Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Q regex escape within variable

by palkia (Monk)
on Mar 28, 2017 at 08:18 UTC ( [id://1186206]=perlquestion: print w/replies, xml ) Need Help??

palkia has asked for the wisdom of the Perl Monks concerning the following question:

Hello.

I tried clustering some file names based on their prefix (those beginning with closers) while ignoring the specific numerical values within them.
To achieve this, whenever a desired prefix pattern appeared, I subbed the numerical values with \d++ so it can be used later as a regex pattern,
however since the prefix contains closers and possibly other special regex characters, I have sandwiched all non-\d++ parts with \Q and \E,
but that raises the error Unrecognized escape \Q passed through in regex;
I can't seem to figure what I am missing ?
... if($fName =~ /^([\[\(].+?[\]\)])/) { #pref detected. generalizing digits my $pref = '^\Q'.$1.'\E'; $pref =~ s/\d++/\\E\\d++\\Q/g; #finding indexes of matches to this prefix my @matchIndexes; for(my $mex=0;$mex<@fNames;$mex++) {if($fNames[$mex] =~ /$pref/i){push(@matchIndexes,$mex);}} ...
Thank you very much for any assistance, and have an awesome day ☺

Update: resolved by quotemeta(), thx to Eily.

Replies are listed 'Best First'.
Re: Q regex escape within variable
by Eily (Monsignor) on Mar 28, 2017 at 08:36 UTC

    \Q interpretation is not done by the regex engine, but by string interpolation. Except you add the \Q in a single quote string, so the interpolation does not happen, and the \Q and \E are still there for the regex engine to read, though they don't have meaning in a regex. If you want to pre-process a string so that its special chars are excaped, you can use quotemeta, which is what \Q does implicitly. my $pref = "^".quotemeta($1);

      Hello Eily.
      Thank you very much for your solution, it seems to be working perfectly after I adapted $pref =~ s/\d++/\\E\\d++\\Q/g; to $pref =~ s/\d++/\\d++/g;
      This would have most likely taken me long to figure out, I'm happy I didn't struggle through it for long before asking for help (like I usually would ^^).
      Thx again :)
Re: Q regex escape within variable
by AnomalousMonk (Archbishop) on Mar 28, 2017 at 08:53 UTC

    It's hard to understand just what you're trying to do. Can you please give a Short, Self-Contained, Correct Example? Giving  $fname a value and populating the  @fNames array with a few typical values along with the corresponding expected contents of the  @matchIndexes array would go a long way toward making things clearer. The line number of the warning message generated by the SSCCE will be helpful.


    Give a man a fish:  <%-{-{-{-<

      Hello.
      I'm sorry if my explanation was insufficiently clear.

      The reason I omitted most of my code as well as the line number, was that experimenting confirmed that the code lines I've posted were the only ones responsible for the error, and independently of $fName's value.

      The error generating part was $fNames[$mex] =~ /$pref/i,
      which was intended to return true when for example $fName began with '[case A.2 v6]' and $fNames[$mex] began with the exact same string except that the numbers could have been any other numbers as well, such as '[case A.99 v0]' or '[Case A.1 v123]' but not '[case B.2 v6]'.

      Once I have constructed the list of indexes of matching names, I could refer to the subarray and work on it specifically, such as ignoring the prefix and sorting the names independently of it.

      I hope this clears things up (but notice the above update saying the problem has been solved already).
      Thank you very much.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1186206]
Approved by Corion
Front-paged by 1nickt
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found