Raku中插入条件代码的正则表达式

9
如何在Raku正则表达式中使用插入式条件代码的正则表达式,作为其Perl正则表达式的类比。
 my $F = 1;
 'foobarbar' =~ / (?(?{  $F  }) foo |  bar ) bar /x  ;

经过一整天的努力却无果,请求帮助,感谢。


首先,您需要使用~~进行正则表达式匹配... 这仍然会出错,但它是一个开始。 - jjmerelo
2个回答

7

这将有效:

my $F=1
'foobar' ~~ / ^^ "{ $F ?? "foo" !! "bar" }" bar /; # 「foobar」
$F=0
'foobar' ~~ / ^^ "{ $F ?? "foo" !! "bar" }" bar /; # Nil

正则表达式中的代码块将被运行,但是除非你通过引号显式地将它们转换为字符串,否则它们将被丢弃。


6

快速回答

my $F = 1;
'foobarbar' ~~ / (<?{  $F  }> foo |  bar ) bar /  ;
say $/; # use `say` to get a "human friendly" `gist` of a value

显示:

「foobar」
 0 => 「foo」

鉴于:

my $F = 0;
'foobarbar' ~~ / (<?{  $F  }> foo |  bar ) bar /  ;
put $/; # use `put` to get a simple computer stringification of a value

显示器:

barbar

更快的答案

请帮我解决问题,我已经尝试了一整天都没有成功,谢谢。

您可以在这里继续提问,我们将尽力在您提问的同一天回答,并且您所提出的问题和我们提供的答案将有助于其他人。感谢您的提问,请继续提问。但是,有更快的方式来获取答案,而且它们通常比我们在这里提供的答案更好:

  1. "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.

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

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

当然,还有许多其他资源(特别是旧的设计推测文档),但希望以上内容能帮助您更快地找到很多问题的答案。祝好运!


天啊...我真不敢相信自己有多无知。非常感谢! - itil memek cantik
不用谢,感谢您的提问。 :) 我最近的大部分(希望是最终版)编辑只是移动了一些内容,可能对您没有太大兴趣。(它们主要是为以后的读者准备的。)但我建议您阅读有关搜索和聊天的部分。 - raiph
1
@itilmemekcantik并不是无知,只是像我们其他人一样在学习! - jubilatious1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接