我正在尝试构建一个正则表达式来匹配包含特殊字符和表情符号的英文文本。我找到了这个用于匹配包含特殊字符的英文的正则表达式:[\u0000-\u007F]+$
;以及这个用于匹配表情符号的正则表达式:([^\x00-\x7F]+\ *(?:[^\x00-\x7F]| )*)
。但是我不知道如何将它们组合在一起,有什么想法吗?
我正在尝试构建一个正则表达式来匹配包含特殊字符和表情符号的英文文本。我找到了这个用于匹配包含特殊字符的英文的正则表达式:[\u0000-\u007F]+$
;以及这个用于匹配表情符号的正则表达式:([^\x00-\x7F]+\ *(?:[^\x00-\x7F]| )*)
。但是我不知道如何将它们组合在一起,有什么想法吗?
^(?:[a-zA-Z]|\P{L})+$
代码示例:
RegExp regex = RegExp(r'^(?:[a-zA-Z]|\P{L})+$', unicode: true);
请参见证明
说明
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?: group, but do not capture (1 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
[a-zA-Z] any character of: 'a' to 'z', 'A' to 'Z'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\P{L} any char other than a Unicode letter
--------------------------------------------------------------------------------
)+ end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
RegExp regex = RegExp(r'^(?:[a-zA-Z]|\P{L})+$', unicode: true);
?参考 Matching Unicode letters with RegExp。 - Ryszard CzechString.matches()
方法而导致的StackOverflow。 - Jura Gorohovsky