我試圖讓一個正則運算式描述一個單引號分隔的字串。在字串內部,我可以有任何可列印(或空白)字符(不是單引號),也可以是一系列兩個單引號,這將是“轉義”單引號。
[[:print:]] 字符類(也寫為 \p{XPosixPrint})符合我想要允許的字符的要求......除了它也允許單個“單引號”(')。這是我不想發生的。
那么,是否有一種簡單的方法可以做到這一點,例如描述一個字符以同時匹配兩個運算式(例如 [[:print:]] 和 [^'] ),或者我是否必須創建一個自定義字符類列舉我允許(或禁止)的一切?
uj5u.com熱心網友回復:
/(?!')\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} ['] ) ])/
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/325972.html
上一篇:如何添加和恢復堆?
下一篇:檔案決議中perl回圈列印的問題
