Capture Single Emoji using Regex

Viewed 99

Of all the Emojis present in a text, I want to capture Strawberry emoji ().

Using Google I narrowed down my expression from

(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])

to

(\ud83c[\ud000-\udfff])

This narrows my search to fewer emojis but still captures others as well:

                 

I found out each emoji has its own range and Unicode, E.G Strawberry () has the range 6107-6108 and Unicode (U+1F353), Apple () has range 6086-6087 and Unicode (U+1F34E).

How do I narrow down my regex further, so that it captures the single Emoji while ignoring the rest?

2 Answers

A resource like unicode-table.com will supply all the information you need about a code-point, including name, glyph representations, and most importantly encoding. For strawberry, you have the following:

Name: Strawberry
Glyph:
Code: 0x1F353
UTF-8: \xF0 \x9F \x8D \x93
UTF-16(BE): \uD83C \uDF53
UTF-32(BE): \U0001F353

How you represent your search string depends on the language/flavor of your regex. For example, regex101.com tends to have minimal support for Unicode, so you have two options:

  1. Literal strawberry:

    //

  2. UTF-16 (big endian):

    /\uD83C\uDF53/

A language like python has a lot more support you can use any of the options in the little table above with the appropriate escapes:

  1. '\N{Strawberry}'
  2. '\U0001F353'
  3. '\uD83C\uDF53'
  4. ''

All of these are equivalent to the literal strawberry glyph: these escapes are parsed by the interpreter, not the regex engine.

The key is that Unicode is an extremely well documented resource. It may seem complicated, and often is, but you'll find that looking up the appropriate documentation is always easier than exhaustive guess-and-check in the long run.

I highly recommend reading Python's Unicode HowTo. It's not exhaustive, but is an excellent resource which will point you in all the right directions.

I don't know if there is an intelligent way to find it but you already narrowed it down a lot. With trial and error I found the exact match :

\ud83c\udf53

See https://regex101.com/r/7psOg1/1

Edit : after some research, it seems you need a unicode to hex converter. There are a lot of them online. Just paste your emoji and the associated unicode will show. For example, for , you get d83eded0 which means \ud83e\uded0 will match Note that you have to choose the unicode 16 encoding.

Related