How can I escape metacharacters in a Raku regex the way I would with Perl's quotemeta function (\Q..\E
)?
That is, the Perl code
my $sentence = 'The quick brown fox jumped over the lazy dog';
my $substring = 'quick.*?fox';
$sentence =~ s{$substring}{big bad wolf};
print $sentence
treats each of .
, *
, and ?
as metacharacters and thus prints The big bad wolf jumped over the lazy dog
. But if I change the second-to-last line to $sentence =~ s{\Q$substring\E}{big bad wolf};
, then Perl treats .*?
as literal characters and thus prints The quick brown fox jumped over the lazy dog
.
How can I treat characters literally in a Raku regex?
You can treat characters in a Raku regex literally by surrounding them with quotes (e.g., '.*?'
) or by using using regular variable interpolation (e.g., $substring
inside the regex where $substring
is a string contaning metacharacters).
Thus, to translate the Perl program with \Q...\E
from your question into Raku, you could write:
my $sentence = 'The quick brown fox jumped over the lazy dog';
my $substring = 'quick.*?fox';
$sentence ~~ s/$substring/big bad wolf/;
print $sentence
This would treat .*?
as literal characters, not metacharacters. If you wanted to avoid interpolation with literal text rather than a variable, you could change the substitution regex to s/quick '.*?' fox/big bad wolf/
. Conversely, if you want to use the $substring
variable as part of a regex (that is, if you do want .*?
to be metacharacters) you'd need to to change the substitution regex to s/<$substring>/big bad wolf/
. For more details, you can consult the Rexex interpolation docs.
What should you do when you don't know how to do something in Raku? Asking either on the IRC channel or here on Stack Overflow is an option – and asking a clear Q on SO has the benefit of making the answer more searchable for anyone else who has the same question in the future.
But both IRC and SO are asynchronous – so you'll probably need to wait a bit for an answer. There are other ways that folks interested in Raku frequently get good/great answers to their questions more easily and quickly than they could from IRC/SO, and the remainder of this answer provides some guidance about these ways. (I've numbered the steps in the general order I'd recommend, but there's no reason you need to follow that order).
Raku strives to have awesome error messages, and sometimes you'll be lucky enough to try something in a way that doesn't work but where Raku can tell what you were trying to do.
In those cases, Raku will just tell you how to do what you wanted to do. And, in fact, \Q...\E
is one such case. If you'd tried to do it the Perl way
/\Q$substring\E/
you'd have gotten the same answer I gave above (use $substring
or quotes) in the form of the following error message:
Unsupported use of \Q as quotemeta. In Raku please use: quotes or
literal variable match.
So, sometimes, Raku will solve the problem for you! But that's not something that will happen all the time and, any time you're tempted to ask a SO question, it's a good bet that Raku didn't answer your question for you. So here are the steps you'd take in that case:
The first true step should, of course, be to search the Raku docs for anything useful. I bet you did this – the docs currently don't return any relevant results for \Q..\E
. In fact, the only true positive match of \Q...\E
in those results is from the Perl to Raku guide - in a nutshell: "using String::ShellQuote
(because \Q…\E
is not completely right) ...". And that's obviously not what you're interested in.
The docs website doesn't always yield a good answer to simple questions. Sometimes, as we clearly see with the \Q...\E
case, it doesn't yield any answer at all for the relevant search term.
Again, you probably did this, but it's good to keep in mind: You can limit your SO search questions/answers tagged as related to Raku by adding [raku]
to your query. Here, a query of [raku] "\Q...\E"
wouldn't have yielded anything relevant – but, thanks to your question, it will in the future :)
Raku's design was written up in a series of "spec" docs written principally by Larry Wall over a 2 decade period.
(The word "specs" is short for "specification speculations". It's both ultra authoritative detailed and precise specifications of the Raku language, authored primarily by Larry Wall himself, and mere speculations -- because it was all subject to implementation. And the two aspects are left entangled, and now out-of-date. So don't rely on them 100% -- but don't ignore them either.)
The "specs", aka design docs, are a fantastic resource. You can search them using google by entering your search terms in the search box at design.raku.org.
A search for \Q...\E
lists 7 pages. The only useful match is Synopsis 5: Regexes and Rules ("24 Jun 2002 — \Q$var\E /
..."). If I click it and then do an in-page search for \Q
, I get 2 matches that, together, answer your question (at least with respect to variables – they don't mention literal strings):
In Raku
/ $var /
is like a Perl/ \Q$var\E /
\Q...\E
sequences are gone.
In this case, searching the design docs answered your question. But what if it hadn't/we didn't understand the answer?
In that case, searching the IRC logs can be a great option (as previously discussed in the Quicker answers section of an answer to a past Q. The IRC logs are an incredibly rich mine of info with outstanding search features. Please read that section for clear general guidance.
In this particular case, if we'd searched for \Q
in the old Raku channel, we would have gotten a bunch of useful matches. None of the first few fully answer your question, but several do (or at least make the answer clear) if read in context – but it's the need to read the surrounding context that makes me put searching the IRC logs below the previous steps.