regex - 有没有办法让字符匹配字符类的结合?
问题描述
我试图让一个正则表达式描述一个单引号分隔的字符串。在字符串中,我可以有任何可打印(或空白)字符(不是单引号),也可以是一系列两个单引号,这将是一个“转义”单引号。
[[:print:]] 字符类(也写为 \p{XPosixPrint})符合我想要允许的字符的要求......除了它还允许单个“单引号”(')。这是我不想发生的。
那么,有没有一种简单的方法可以做到这一点,比如描述一个字符以同时匹配两个表达式(如 [[:print:]] 和 [^'] ),还是我必须创建一个自定义字符类列举我允许(或禁止)的一切?
解决方案
/(?!')\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}
。
-
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}']
\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} + ['] ) ])/
推荐阅读
- python - Python:无法将文件拆分为多个文件
- mysql - SQL 查询中的 case 语句
- excel - 循环复制/粘贴,直到等于最后使用的列
- python - 用于编辑 csv 的简单“查找和替换”Python 脚本会出错
- docker - WSL2 中的 Kubernetes 挂载文件夹
- postgresql - MyBatis 中的解析错误插入到带有静态值的表选择中
- c++ - 仅当类型为 std::complex 时才缩放
- node.js - 如何为猫鼬模型的对象设置对象属性是唯一的?
- android - PreferenceFragment 已弃用
- automation - 如何自动化 Visual Studio 发布和附加调试器?