Ahmed Nabil
Ahmed Nabil

Reputation: 751

Regex to match English, special characters and emojis

I'm trying to build a regex to match English with special characters and also the emojis, I found this one [\u0000-\u007F]+$ for English with special characters and this one ([^\x00-\x7F]+\ *(?:[^\x00-\x7F]| )*) for emojis but I can't figure how to combine both, any idea how?.

Upvotes: 3

Views: 1766

Answers (1)

Ryszard Czech
Ryszard Czech

Reputation: 18641

If you need to match any string that cannot contain any non-English letter use

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

Code sample:

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

See proof

Explanation

                         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

Upvotes: 5

Related Questions