> On Sat, Dec 7, 2019 at 12:51 AM ToddAndMargo via perl6-users
> <perl6-us...@perl.org <mailto:perl6-us...@perl.org>> wrote:
>
> Hi All,
>
> Is there a `Q[]` that can be used in a regex?
>
> I am looking for how to get around
>
> my $x = Q[\:\\::]; ( my $y = $x ) ~~ s/ '\\\\' /x/; say $y
> \:x::
>
> This does not work:
> my $x = Q[\:\\::]; ( my $y = $x ) ~~ s/ Q[\\] /x/; say $y
> \:\\::
>
> Nor does this:
> my $x = Q[\:\\::]; ( my $y = $x ) ~~ s/ [\\] /x/; say $y
> x:\\::
>
> Many thanks,
> -T
On 2019-12-07 07:02, Brad Gilbert wrote:
The shortcut spelling of Q[…] is to use 「 and 」 (U+FF62 and U+FF63)
my $x = 「\:\\::」; ( my $y = $x ) ~~ s/ 「\\」 /x/; say $y
\:\\::
The case could be made that \Q[\\] should work as well. (It would need
to be added).
(Along with \q[…] and \qq[…])
Note that \Q[…] doesn't work in string literals currently either. While
\q[…] and \qq[…] do.
> "\q[\n]\n"
\n
> '\n\qq[\n]'
\n
Note that single and double quotes also work in regexs.
The three of them ('…' "…" 「…」) have a few jobs.
1. They escape spaces and other non-alphanumerics.
> 'a b c' ~~ / 'a b c' /
Nil
> 'a b c A B C' ~~ / :i 'a b c' /
A B C
> 'a b c' ~~ / 'a . c' /
Nil
> 'a . c' ~~ / 'a . c' /
a . c
Note that the rules for the string literal still apply.
> "abc\n" ~~ / 'abc\n' /
Nil
> "abc\n" ~~ / "abc\n" /
abc
2. They group characters as a single atom.
(Meaning they behave a bit like [] in a regex)
> 'abccd' ~~ / 'abc'+ d /
Nil
> 'abccd' ~~ / [abc]+ d /
Nil
> 'abccd' ~~ / abc+ d /
abccd
> 'abccd abcABCabcd' ~~ / :i 'abc'+ d /
abcABCabcd
> 'abccd abcABCabcd' ~~ / :i [abc]+ d /
abcABCabcd
Note that '…' in a regex behaves like '…' outside of one, as well as "…"
behaving like "…" and 「…」 behaving like 「…」
Hi Brad,
That was above and beyond! Thank you!
my $x = Q[\:\\::]; ( my $y = $x ) ~~ s/ 「\\」 /x/; say $y
\:x::
What is the easiest way to get those weird brackets in Fedora31?
-T