如何在 Raku 正则表达式中插入条件代码
正则表达式 与 Perl 正则表达式类似
my $F = 1;
'foobarbar' =~ / (?(?{ $F }) foo | bar ) bar /x ;
Run Code Online (Sandbox Code Playgroud)
辛苦了一天了,没有结果,请大家帮忙,谢谢。
这将起作用:
\nmy $F=1\n'foobar' ~~ / ^^ "{ $F ?? "foo" !! "bar" }" bar /; # \xef\xbd\xa2foobar\xef\xbd\xa3\n$F=0\n'foobar' ~~ / ^^ "{ $F ?? "foo" !! "bar" }" bar /; # Nil\nRun Code Online (Sandbox Code Playgroud)\n正则表达式中的代码块将被运行,但除非您将它们显式转换为字符串(通过引号),否则它们将被丢弃。
\nmy $F = 1;\n\'foobarbar\' ~~ / (<?{ $F }> foo | bar ) bar / ;\nsay $/; # use `say` to get a "human friendly" `gist` of a value\nRun Code Online (Sandbox Code Playgroud)\ndisplays:
\n\xef\xbd\xa2foobar\xef\xbd\xa3\n 0 => \xef\xbd\xa2foo\xef\xbd\xa3\nRun Code Online (Sandbox Code Playgroud)\nwhereas:
\nmy $F = 0;\n\'foobarbar\' ~~ / (<?{ $F }> foo | bar ) bar / ;\nput $/; # use `put` to get a simple computer stringification of a value\nRun Code Online (Sandbox Code Playgroud)\ndisplays:
\nbarbar\nRun Code Online (Sandbox Code Playgroud)\n\n\nPlease help out after tried so hard a day to no avail, thanks.
\n
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:
\n"Chat" (Even if you don\'t like participating in real-time discussions, still read Chat logs below.)
\nIf you like real-time discussion, there are IRC, Discord etc channels.
\nYou 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.
\nChat logs
\nChat 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.
\nFor 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.)
\nIn 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.
\nDoc如果您想搜索和阅读文档,请按照下面的指南从主文档网站doc.raku.org快速获得许多问题的答案:
\n搜索文档网站的搜索框(网站右上角)比看起来更有用。您可能不知道要输入什么。即使您知道要输入什么,它也可能不在文档中。即使它在文档中,也可能不会包含在匹配项的下拉列表中。但您仍然应该尝试,因为有一个经常被忽视的“搜索整个网站”选项 listed at the very bottom of the drop-down (after all the listed matches).
\n例如,如果您condition在搜索框中键入内容,然后选择“在整个网站中搜索条件”条目,您将看到文档网站上的匹配项,如 google 所见。如果您随后浏览它们,您将看到该<?{condition}> yes-pattern | no-pattern示例。只需弹出搜索框,您就可以在大约两分钟内找到问题的答案!condition
顺便说一句,如果搜索没有 work for you, please feel free to provide feedback about what you tried and failed to find. (And thanks if you do.)
\n阅读有时搜索不会给你答案,但它仍然值得阅读文档,因为你只是没有在搜索框中使用正确的单词。要快速访问文档网站中与您的问题相关的最重要部分,尤其是如何将另一个 PL 的知识转移到 Raku,请单击文档网站顶部绿色区域中的“语言”一词。这将带您进入Raku 语言选项卡,其中包含以下重要信息:
\n有“Language X to Raku”迁移指南,展示了如何在 Raku 中执行与在其他 PL 中执行的操作相同的操作。您绝对应该利用 Perl to Raku 指南。(我们也非常感谢对此的反馈。)到目前为止,它分为六个部分;简而言之,您应该从 Perl 到 Raku 指南开始。在其中您将看到特殊匹配器通常属于<>语法部分;它提供了这个示例(稍作修改):
(?{condition)) yes-pattern | no-pattern # Perl\nbecomes\n<?{condition}> yes-pattern | no-pattern # Raku\nRun Code Online (Sandbox Code Playgroud)\n还有详细的语言参考信息。语言选项卡的基础主题部分包括正则表达式页面;在其中您将找到一个正则表达式布尔条件检查部分,其中包含完全专用于您所询问的内容的整个部分。
\n当然还有更多资源(尤其是旧的设计推测文档),但希望上述内容能够帮助您更快地找到许多问题的答案。祝你好运!
\n