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

问题描述

我正在尝试建立一个正则表达式以匹配具有特殊字符的英语和表情符号,我发现此[\u0000-\u007F]+$用于具有特殊字符的英语,而此([^\x00-\x7F]+\ *(?:[^\x00-\x7F]| )*)用于表情符号,但是我可以知道如何将两者结合起来,知道怎么做吗?。

解决方法

如果您需要匹配任何不能包含任何非英语字母的字符串

^(?:[a-zA-Z]|\P{L})+$

代码示例:

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

请参见proof

说明

                         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