首页 > 解决方案 > 有没有办法让字符匹配字符类的结合?

问题描述

我试图让一个正则表达式描述一个单引号分隔的字符串。在字符串中,我可以有任何可打印(或空白)字符(不是单引号),也可以是一系列两个单引号,这将是一个“转义”单引号。

[[:print:]] 字符类(也写为 \p{XPosixPrint})符合我想要允许的字符的要求......除了它还允许单个“单引号”(')。这是我不想发生的。

那么,有没有一种简单的方法可以做到这一点,比如描述一个字符以同时匹配两个表达式(如 [[:print:]] 和 [^'] ),还是我必须创建一个自定义字符类列举我允许(或禁止)的一切?

标签: regexperl

解决方案


/(?!')\p{Print}/                     # Worst performance and kinda yuck?
/\p{Print}(?<!')/                    # Better performance but yuckier?
/[^\P{Print}']/                      # Best performance, but hard to parse.[1]
use experimental qw( regex_sets );   # No idea why still experimental.
/(?[ \p{Print} - ['] ])/             # Best performance and clearest.
/[^\p{Cn}\p{Co}\p{Cs}\p{Cc}']/       # Non-general solution.
                                     # Best performance but fragile.[2]

\p{Print}是 的别名\p{XPosixPrint}


  1.    char that is (printable and not('))
     = char that is (not(not(printable and not('))))
     = char that is (not(not(printable) or not(not('))))
     = char that is (not(not(printable) or '))
     = [^\P{Print}']
    
  2. \p{Print}包括除未分配、私人使用、代理和控制字符之外的所有字符。

    /[^\p{Cn}\p{Co}\p{Cs}\p{Cc}']/
    

    简称

    /[^\p{General_Category=Unassigned}\p{General_Category=Private_Use}\p{General_Category=Surrogates}\p{General_Category=Control}']/
    

    或者

    use experimental qw( regex_sets );   # No idea why still experimental.
    /(?[ !(
         \p{General_Category=Unassigned}
       + \p{General_Category=Private_Use}
       + \p{General_Category=Surrogates}
       + \p{General_Category=Control}
       + [']
    ) ])/
    

推荐阅读