How to do the inserted conditional code regex in Raku regex
As analogue to its Perl regex
my $F = 1;
'foobarbar' =~ / (?(?{ $F }) foo | bar ) bar /x ;
Please help out after tried so hard a day to no avail, thanks.
How to do the inserted conditional code regex in Raku regex
As analogue to its Perl regex
my $F = 1;
'foobarbar' =~ / (?(?{ $F }) foo | bar ) bar /x ;
Please help out after tried so hard a day to no avail, thanks.
This will work:
my $F=1
'foobar' ~~ / ^^ "{ $F ?? "foo" !! "bar" }" bar /; # 「foobar」
$F=0
'foobar' ~~ / ^^ "{ $F ?? "foo" !! "bar" }" bar /; # Nil
Code blocks in regexes will be run, but unless you convert them explicitly to strings (via quotes) they will be discarded.
my $F = 1;
'foobarbar' ~~ / (<?{ $F }> foo | bar ) bar / ;
say $/; # use `say` to get a "human friendly" `gist` of a value
displays:
「foobar」
0 => 「foo」
whereas:
my $F = 0;
'foobarbar' ~~ / (<?{ $F }> foo | bar ) bar / ;
put $/; # use `put` to get a simple computer stringification of a value
displays:
barbar
Please help out after tried so hard a day to no avail, thanks.
You are more than welcome to keep asking questions here on SO. We will do our best to answer the same day you ask, and the Qs you ask and answers we provide will help everyone. So, thanks for asking, and keep them coming. That said, there are much quicker ways to get answers, and they're typically better answers than we can provide here:
"Chat" (Even if you don't like participating in real-time discussions, still read Chat logs below.)
If you like real-time discussion, there are IRC, Discord etc channels.
You can ask or answer questions, or more generally enjoy yourself, in real-time, right now, by clicking web page that will take you to the #raku-beginners
IRC channel to visit a Raku noob "chat" channel. If any Rakoons are around (we're currently mostly English speaking folk living in Europe or the US, though that'll hopefully expand in coming years), you'll typically get friendly engagement in a few minutes.
There are several other channels. Click the logs link below to see a list.
Chat logs
Chat channels are (normally) publicly logged and searchable. More than a dozen mines containing something like a million diamonds in the rough -- comments made by Rakoons and visitors in real time continuously since 2005.
For many purposes, searching these logs is vastly richer pickings than googling (which is frequently useless). For example, googling the doc site for matches of \Q
(a Perl escape that has a Raku equivalent) lists 50 false positives with just one true positive, whereas a search for \Q
in the old Raku channel displays a long list of matches, and my brief review of them suggests many of them are useful.
Search features include filtering by nick. For example, a search for comments by TimToady
(Larry Wall) containing the word macro
.
You can even use Raku regexes! (If you do, please be thoughtful. For example, to avoid timeouts you may need to break a search into several submissions, each spanning less than 15 years.)
In summary, you can not only search nearly two decades worth of Rakoons and non Rakoon visitors making unimaginably terrible jokes while productively discussing every bit of code (in Raku or any other PL) and every Raku topic that anyone has cared to discuss, but also search with precision to keep the signal to noise ratio high.
Doc If you want to search and read documentation, follow my guide below to quickly get answers to many questions from doc.raku.org, the main doc website:
Search The doc site's search box (top right of the website) is more useful than it might seem. You may not know what to type in. Even if you know what to type in, it may not be in the doc. Even if it is in the doc, it might not be included in the drop-down list of matches. But you should still try because there's an often overlooked "search the entire site" option listed at the very bottom of the drop-down (after all the listed matches).
For example, if you type condition
into the search box, and select the "Search the entire site for condition" entry, you'll see the matches on the doc site as seen by google. If you then browse through them you'll see the <?{condition}> yes-pattern | no-pattern
example. You could have found the answer to your Q in about two minutes flat by just popping condition
in the search box!
BTW, if searching does not work for you, please feel free to provide feedback about what you tried and failed to find. (And thanks if you do.)
Read Sometimes search won't get you the answer but it's still worth reading doc because you just aren't using the right words in the search box. For quick access to the most important parts of the doc website relevant to questions like yours -- especially how to transfer knowledge of another PL to Raku -- click on the word Language in the green area at the top of the doc site. This will take you to the Raku Language tab, which includes great info as follows:
There are "Language X to Raku" Migration guides that show how to do things in Raku that are equivalent to doing them in some other PL. You should definitely take advantage of The Perl to Raku guide. (We'd appreciate feedback about it too.) It's broken into six parts so far; you should start with the Perl to Raku guide - in a nutshell. In it you'll see a Special matchers generally fall under the <>
syntax section; it provides this example (slightly modified):
(?{condition)) yes-pattern | no-pattern # Perl
becomes
<?{condition}> yes-pattern | no-pattern # Raku
There's also detailed language reference info. The Language tab's Fundamental topics section includes a Regexes page; in it you'll find a Regex Boolean condition check section that contains a whole section entirely dedicated to what you've asked about.
There are of course many more resources (especially the old design speculation docs), but hopefully the above will help you find answers to many questions more quickly. Good luck!