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

3

我正在尝试构建一个正则表达式来匹配包含特殊字符和表情符号的英文文本。我找到了这个用于匹配包含特殊字符的英文的正则表达式:[\u0000-\u007F]+$;以及这个用于匹配表情符号的正则表达式:([^\x00-\x7F]+\ *(?:[^\x00-\x7F]| )*)。但是我不知道如何将它们组合在一起,有什么想法吗?


你使用 JavaScript 吗? - Wiktor Stribiżew
不,我想在Dart代码中使用这个正则表达式。 - Ahmed Nabil
https://apps.timwhitlock.info/emoji/tables/unicode - 所以这取决于编码,对吧?https://apps.timwhitlock.info/unicode/inspect/hex/1F601 - JGFMK
这两个部分几乎是彼此的对立面(除了空格)。你所说的“表情符号”实际上是任何不是 ASCII 0-127 的东西。 - Bohemian
没错,我只是想将它们组合起来,以便能够排除其他语言的字符。 - Ahmed Nabil
1个回答

5
如果您需要匹配任何不包含非英文字母的字符串,请使用:
^(?:[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

我认为 regex101 是有 bug 的,而不是 regexr。 - Ahmed Nabil
@TheForgottenWarrior 我也无法在regexr中使其工作。这一定是个bug吧。 - Ryszard Czech
1
@TheForgottenWarrior 你试过这样写吗 RegExp regex = RegExp(r'^(?:[a-zA-Z]|\P{L})+$', unicode: true);?参考 Matching Unicode letters with RegExp - Ryszard Czech
把英语仅视为ASCII字母可能有点天真:https://en.wikipedia.org/wiki/English_terms_with_diacritical_marks - lrn
很好,运行良好,谢谢。在对该正则表达式进行匹配之前,检查字符串的长度不会有任何损失。我刚刚不得不调试一个由于将8K+字符字符串传递给Java的String.matches()方法而导致的StackOverflow。 - Jura Gorohovsky
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接