9mm
Help converting regex (to remove emojis)
I’m trying to make a regex to remove emojis based on this thread:
This causes an error:
Regex.replace(~r/[\u{1F600}-\u{1F6FF}]/, "💰 Monies! 💲", "")
# (Regex.CompileError) PCRE does not support \L, \l, \N{name}, \U, or \u at position 2
I tried using \x{FFFF} syntax instead according to this: Regex Tutorial - Unicode Characters and Properties
But I get another error:
Regex.replace(~r/[\x{1F600}-\x{1F6FF}]/, "💰 Monies! 💲", "")
# (Regex.CompileError) character value in \x{} or \o{} is too large at position 9
Apparently I also need u to enable unicode but that doesnt seem to work either:
Regex.replace(~r/[\x{1F600}-\x{1F6FF}]/u, "💰 Monies! 💲", "")
"💰 Monies! 💲"
Most Liked
kip
You might have some better luck using the unicode character class So (other symbol). For example:
iex> x = "💰 Monies! 💲"
"💰 Monies! 💲"
iex> String.replace x, ~r/\p{So}/u, ""
" Monies! "
BTW, the “correct” approach to this would be:
iex> String.replace x, ~r/\p{Emoji}/u, ""
But erlang’s re module doesn’t support that character class.
mudasobwa
There are many issues here. Both characters above are not in the range you specified in the first place.
▶ to_charlist "💰"
#⇒ [128176]
▶ to_charlist "💲"
#⇒ [128178]
▶ 0x1F600
#⇒ 128512
To make it work for the range with \u, just interpolate the literals (I voluntarily changed the starting value for the range to what it probably should be):
▶ Regex.replace(~r/[#{"\u{1F000}"}-#{"\u{1F6FF}"}]/u, "💰 Monies! 💲", "")
" Monies! "
\x will work out of the box with a proper range:
Regex.replace(~r/[\x{1F000}-\x{1F6FF}]/u, "💰 Monies! 💲", "")
" Monies! "
kip
Try String.codepoints/1. Where the code point can’t be decoded to UTF-8 you’ll see a number.
iex> String.codepoints x
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ".", ".", ".", ".", "0", "1",
"2", "3", "4", "5", "6", "7", "8", "9", " ", "不", "極", ",", "物", "片",
"類", "書", "車", "裡", ...]
If you want to get the numeric value, add <<0>> to the string:
iex> x <> <<0>>
<<97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 46, 46, 46, 46, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 32, 228, 184, 141, 230, 165, 181, 239, 188, 140,
...>>
If you want the raw integers underneath use to_charlist/1:
iex> to_charlist x
[97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 46, 46, 46, 46, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 32, 19981, 26997, 65292, 29289, 29255, 39006,
26360, 36554, 35041, ...]
kip
Yes, I see the same. It’s a bit surprising since according to some utility code I wrote they look to be So but re certainly doesn’t agree:
iex> x
"🤨🧐🤓🤩"
iex> Cldr.Unicode.Category.category x
[:So, :So, :So, :So]
iex> String.replace x, ~r/\p{So}/u, ""
"🤨🧐🤓🤩"
kip
Its possible the range you want is a little broader than @mudasobwa suggested (depends on your requirements). The nearest I can establish the relevant unicode blocks are:
1F000…1F02F; Mahjong Tiles
1F030…1F09F; Domino Tiles
1F0A0…1F0FF; Playing Cards
1F100…1F1FF; Enclosed Alphanumeric Supplement
1F200…1F2FF; Enclosed Ideographic Supplement
1F300…1F5FF; Miscellaneous Symbols and Pictographs
1F600…1F64F; Emoticons
1F650…1F67F; Ornamental Dingbats
1F680…1F6FF; Transport and Map Symbols
1F700…1F77F; Alchemical Symbols
1F780…1F7FF; Geometric Shapes Extended
1F800…1F8FF; Supplemental Arrows-C
1F900…1F9FF; Supplemental Symbols and Pictographs
So perhaps ~r/[\x{1F000}-\x{1F9FF}]/u would be an alternative.







