kandi background
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Regex open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Regex

Named Capturing Groups

Hyperscan 5.4.0

path-to-regexp

Named Capturing Groups

re2

hyperscan

Hyperscan 5.4.0

xregexp

JavaVerbalExpressions

Popular Libraries in Regex

Trending New libraries in Regex

Top Authors in Regex

1

36 Libraries

641

2

30 Libraries

590

3

24 Libraries

2348

4

20 Libraries

3065

5

15 Libraries

192

6

13 Libraries

19204

7

12 Libraries

3025

8

9 Libraries

461

9

8 Libraries

9413

10

7 Libraries

308

1

36 Libraries

641

2

30 Libraries

590

3

24 Libraries

2348

4

20 Libraries

3065

5

15 Libraries

192

6

13 Libraries

19204

7

12 Libraries

3025

8

9 Libraries

461

9

8 Libraries

9413

10

7 Libraries

308

Trending Kits in Regex

No Trending Kits are available at this moment for Regex

Trending Discussions on Regex

    Escaping metacharacters in a Raku regex (like Perl's quotemeta() or \Q...\E)?
    python-docx adding bold and non-bold strings to same cell in table
    What Raku regex modifier makes a dot match a newline (like Perl's /s)?
    How can the Raku behavior on capturing group in alternate be the same as Perl
    Difference in Perl regex variable $+{name} and $-{name}
    Combine 2 string columns in pandas with different conditions in both columns
    Lookaround regex and character consumption
    Parsing binary files in Raku
    Regex to match nothing but zeroes after the first zero
    python regex where a set of options can occur at most once in a list, in any order

QUESTION

Escaping metacharacters in a Raku regex (like Perl's quotemeta() or \Q...\E)?

Asked 2022-Mar-29 at 23:38

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

1my $sentence = 'The quick brown fox jumped over the lazy dog';
2my $substring = 'quick.*?fox';
3$sentence =~ s{$substring}{big bad wolf};
4print $sentence
5

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?

ANSWER

Answered 2022-Feb-10 at 00:03

Your question's answer:

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:

copy icondownload icon

1my $sentence = 'The quick brown fox jumped over the lazy dog';
2my $substring = 'quick.*?fox';
3$sentence =~ s{$substring}{big bad wolf};
4print $sentence
5my $sentence = 'The quick brown fox jumped over the lazy dog';
6my $substring = 'quick.*?fox';
7$sentence ~~ s/$substring/big bad wolf/;
8print $sentence
9

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.

How you could have found this answer without waiting for SO

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).

Easily get better answers more quickly than asking SO Qs

Step -1: Let Raku answer the question for you

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

copy icondownload icon

1my $sentence = 'The quick brown fox jumped over the lazy dog';
2my $substring = 'quick.*?fox';
3$sentence =~ s{$substring}{big bad wolf};
4print $sentence
5my $sentence = 'The quick brown fox jumped over the lazy dog';
6my $substring = 'quick.*?fox';
7$sentence ~~ s/$substring/big bad wolf/;
8print $sentence
9/\Q$substring\E/
10

you'd have gotten the same answer I gave above (use $substring or quotes) in the form of the following error message:

copy icondownload icon

1my $sentence = 'The quick brown fox jumped over the lazy dog';
2my $substring = 'quick.*?fox';
3$sentence =~ s{$substring}{big bad wolf};
4print $sentence
5my $sentence = 'The quick brown fox jumped over the lazy dog';
6my $substring = 'quick.*?fox';
7$sentence ~~ s/$substring/big bad wolf/;
8print $sentence
9/\Q$substring\E/
10Unsupported use of \Q as quotemeta.  In Raku please use: quotes or
11literal variable match.
12

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:

Step 0: check the docs

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.

Step 1: Search Stack Overflow

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 :)

Step 2: Archived/historical "spec" docs

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.

Step 3: IRC chat logs

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.

Source https://stackoverflow.com/questions/71057626

Community Discussions contain sources that include Stack Exchange Network

    Escaping metacharacters in a Raku regex (like Perl's quotemeta() or \Q...\E)?
    python-docx adding bold and non-bold strings to same cell in table
    What Raku regex modifier makes a dot match a newline (like Perl's /s)?
    How can the Raku behavior on capturing group in alternate be the same as Perl
    Difference in Perl regex variable $+{name} and $-{name}
    Combine 2 string columns in pandas with different conditions in both columns
    Lookaround regex and character consumption
    Parsing binary files in Raku
    Regex to match nothing but zeroes after the first zero
    python regex where a set of options can occur at most once in a list, in any order

QUESTION

Escaping metacharacters in a Raku regex (like Perl's quotemeta() or \Q...\E)?

Asked 2022-Mar-29 at 23:38

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

1my $sentence = 'The quick brown fox jumped over the lazy dog';
2my $substring = 'quick.*?fox';
3$sentence =~ s{$substring}{big bad wolf};
4print $sentence
5

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?

ANSWER

Answered 2022-Feb-10 at 00:03

Your question's answer:

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:

copy icondownload icon

1my $sentence = 'The quick brown fox jumped over the lazy dog';
2my $substring = 'quick.*?fox';
3$sentence =~ s{$substring}{big bad wolf};
4print $sentence
5my $sentence = 'The quick brown fox jumped over the lazy dog';
6my $substring = 'quick.*?fox';
7$sentence ~~ s/$substring/big bad wolf/;
8print $sentence
9

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.

How you could have found this answer without waiting for SO

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).

Easily get better answers more quickly than asking SO Qs

Step -1: Let Raku answer the question for you

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

copy icondownload icon

1my $sentence = 'The quick brown fox jumped over the lazy dog';
2my $substring = 'quick.*?fox';
3$sentence =~ s{$substring}{big bad wolf};
4print $sentence
5my $sentence = 'The quick brown fox jumped over the lazy dog';
6my $substring = 'quick.*?fox';
7$sentence ~~ s/$substring/big bad wolf/;
8print $sentence
9/\Q$substring\E/
10

you'd have gotten the same answer I gave above (use $substring or quotes) in the form of the following error message:

copy icondownload icon

1my $sentence = 'The quick brown fox jumped over the lazy dog';
2my $substring = 'quick.*?fox';
3$sentence =~ s{$substring}{big bad wolf};
4print $sentence
5my $sentence = 'The quick brown fox jumped over the lazy dog';
6my $substring = 'quick.*?fox';
7$sentence ~~ s/$substring/big bad wolf/;
8print $sentence
9/\Q$substring\E/
10Unsupported use of \Q as quotemeta.  In Raku please use: quotes or
11literal variable match.
12

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:

Step 0: check the docs

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.

Step 1: Search Stack Overflow

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 :)

Step 2: Archived/historical "spec" docs

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.

Step 3: IRC chat logs

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.

Source https://stackoverflow.com/questions/71057626

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Regex

Tutorials and Learning Resources are not available at this moment for Regex

Share this Page

share link

Get latest updates on Regex

  • © 2022 Open Weaver Inc.