I would like to restrict Emojis in a text box of range u2600-u26FF using Regex.
I tried this, but fails.
private static readonly Regex regexEmoji = new Regex(@"[\u1F600-\u1F6FF]|[\u2600-\u26FF]");
I would like to restric user from adding Emojis in WP8
Because .NET doesn't support surrogated pairs in Regexes. You have to decompose them manually. To make it clear, a char
in .NET is 16 bits, but 1F600
needs two char
. So the solution is to decompose them "manually".
private static readonly Regex regexEmoji = new Regex(@"\uD83D[\uDE00-\uDEFF]|[\u2600-\u26FF]");
I hope I have decomposed them correctly.
I used this site: http://www.trigeminal.com/16to32AndBack.asp
to decompose the low and the high range \u1F600 == \uD83D \uDE00
, \u1F6FF == \uD83D \uDEFF
. The first part of the surrogate pair is "fixed": \uD83D
, the other is a range.
Example code (http://ideone.com/0o6qbt)
string str = "Hello world 😀🙏☀⛿"; // 🌀 1F600 GRINNING FACE, 1F64F PERSON WITH FOLDED HANDS, 2600 BLACK SUN WITH RAYS, 26FF WHITE FLAG WITH HORIZONTAL MIDDLE BLACK STRIPE
Regex regexEmoji = new Regex(@"\uD83D[\uDE00-\uDEFF]|[\u2600-\u26FF]");
MatchCollection matches = regexEmoji.Matches(str);
int count = matches.Count;
Console.WriteLine(count);
If you want the range 1F300-1F6FF
... it's D83C DF00
to D83C DFFF
and D83D uDC00
to D83D DEFF
string str = "Hello world 🌀😀🙏☀⛿"; // 1F300 CYCLONE, 1F600 GRINNING FACE, 1F64F PERSON WITH FOLDED HANDS, 2600 BLACK SUN WITH RAYS, 26FF WHITE FLAG WITH HORIZONTAL MIDDLE BLACK STRIPE
Regex regexEmoji = new Regex(@"\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDEFF]|[\u2600-\u26FF]");