【问题标题】:perl6 negating user-defined caracter classperl6 否定用户定义的字符类
【发布时间】:2017-06-11 19:14:21
【问题描述】:

我试图忽略所有带有引号的行,不知何故它有点令人困惑:

> my $y='\"\""';
\"\""
> so $y ~~ m/<-[\"]>/
True                      # $y has a " mark, so I want it to be False
> $y ~~ m/<-[\"]>/
「\」
>  $y ~~ m:g/<-[\"]>/
(「\」 「\」)
> $y ~~ m:g/<-["]>/
(「\」 「\」)
$y ~~ m/<-[\\]>/
「"」
> $y ~~ m/<-[\\\"]>/
False

和 一样吗?

> say '"in quotes"' ~~ / '"' <-[ " ]> * '"'/;
「"in quotes"」
> say 'no "foo" quotes' ~~ /  <-[ " ]> + /;
「no 」
> say 'no "foo" quotes' ~~ /  <-[ \" ]> + /;
「no 」

在 perl6 文档示例中,https://docs.perl6.org/language/regexes#Wildcards_and_character_classes,作者不必转义引号;但是,我必须逃避它才能工作, ,即逃避 \ 和逃避“。我误会了什么?

谢谢!!

【问题讨论】:

    标签: class raku negation


    【解决方案1】:

    您不需要在字符类规范中转义字符,除了反斜杠本身:因此指定&lt;-[\"]&gt; &lt;-["]&gt; 相同。而指定&lt;-[\\"]&gt;,则表示所有字符除了\"

    但是,您可能有一种更简单的方法:如果您只在字符串中查找单个(一组)字符,则可以使用 contains

    my $y = "foo bar baz";
    say $y.contains("oo");    # True
    

    这通过使用单个简单的低级字符串匹配绕过了所有昂贵的正则表达式/语法机制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多