正则表达式匹配英语、特殊字符和表情符号

The*_*ior 3 regex regex-group dart

我正在尝试构建一个正则表达式来匹配带有特殊字符的英语以及表情符号,我发现这个正则表达式[\u0000-\u007F]+$用于带有特殊字符的英语,而这个正则([^\x00-\x7F]+\ *(?:[^\x00-\x7F]| )*)表达式用于表情符号,但我不知道如何将两者结合起来,知道如何组合吗?

Rys*_*ech 5

如果您需要匹配任何不能包含任何非英文字母的字符串,请使用

^(?:[a-zA-Z]|\P{L})+$
Run Code Online (Sandbox Code Playgroud)

代码示例:

RegExp regex = RegExp(r'^(?:[a-zA-Z]|\P{L})+$', unicode: true);
Run Code Online (Sandbox Code Playgroud)

查看证明

解释

                         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
Run Code Online (Sandbox Code Playgroud)