What is the regex to extract all the emojis from a string?
I have a String encoded in UTF-8. For example:
Thats a nice joke πππ π
I have to extract all the emojis present in the sentence. And the emoji could be any
When this sentence is viewed in terminal using command less text.txt
it is viewed as:
Thats a nice joke <U+1F606><U+1F606><U+1F606> <U+1F61B>
This is the corresponding UTF code for the emoji. All the codes for emojis can be found at emojitracker.
For the purpose of finding all the occurances, I used a regular expression pattern (<U\+\w+?>)
but it didnt work for the UTF-8 encoded string.
Following is my code:
String s="Thats a nice joke πππ π";
Pattern pattern = Pattern.compile("(<U\\+\\w+?>)");
Matcher matcher = pattern.matcher(s);
List<String> matchList = new ArrayList<String>();
while (matcher.find()) {
matchList.add(matcher.group());
}
for(int i=0;i<matchList.size();i++){
System.out.println(matchList.get(i));
}
This pdf says Range: 1F300β1F5FF for Miscellaneous Symbols and Pictographs
. So I want to capture any character lying within this range.
Solution 1:
Using emoji-java i've wrote a simple method that removes all emojis including fitzpatrick modifiers. Requires an external library but easier to maintain than those monster regexes.
Use:
String input = "A string πwith a \uD83D\uDC66\uD83C\uDFFFfew πemojis!";
String result = EmojiParser.removeAllEmojis(input);
emoji-java maven installation:
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>3.1.3</version>
</dependency>
gradle:
implementation 'com.vdurmont:emoji-java:3.1.3'
EDIT: previously submitted answer was pulled into emoji-java source code.
Solution 2:
the pdf that you just mentioned says Range: 1F300β1F5FF for Miscellaneous Symbols and Pictographs. So lets say I want to capture any character lying within this range. Now what to do?
Okay, but I will just note that the emoji in your question are outside that range! :-)
The fact that these are above 0xFFFF
complicates things, because Java strings store UTF-16. So we can't just use one simple character class for it. We're going to have surrogate pairs. (More: http://www.unicode.org/faq/utf_bom.html)
U+1F300 in UTF-16 ends up being the pair \uD83C\uDF00
; U+1F5FF ends up being \uD83D\uDDFF
. Note that the first character went up, we cross at least one boundary. So we have to know what ranges of surrogate pairs we're looking for.
Not being steeped in knowledge about the inner workings of UTF-16, I wrote a program to find out (source at the endΒ β I'd double-check it if I were you, rather than trusting me). It tells me we're looking for \uD83C
followed by anything in the range \uDF00-\uDFFF
(inclusive), or \uD83D
followed by anything in the range \uDC00-\uDDFF
(inclusive).
So armed with that knowledge, in theory we could now write a pattern:
// This is wrong, keep reading
Pattern p = Pattern.compile("(?:\uD83C[\uDF00-\uDFFF])|(?:\uD83D[\uDC00-\uDDFF])");
That's an alternation of two non-capturing groups, the first group for the pairs starting with \uD83C
, and the second group for the pairs starting with \uD83D
.
But that fails (doesn't find anything). I'm fairly sure it's because we're trying to specify half of a surrogate pair in various places:
Pattern p = Pattern.compile("(?:\uD83C[\uDF00-\uDFFF])|(?:\uD83D[\uDC00-\uDDFF])");
// Half of a pair --------------^------^------^-----------^------^------^
We can't just split up surrogate pairs like that, they're called surrogate pairs for a reason. :-)
Consequently, I don't think we can use regular expressions (or indeed, any string-based approach) for this at all. I think we have to search through char
arrays.
char
arrays hold UTF-16 values, so we can find those half-pairs in the data if we look for it the hard way:
String s = new StringBuilder()
.append("Thats a nice joke ")
.appendCodePoint(0x1F606)
.appendCodePoint(0x1F606)
.appendCodePoint(0x1F606)
.append(" ")
.appendCodePoint(0x1F61B)
.toString();
char[] chars = s.toCharArray();
int index;
char ch1;
char ch2;
index = 0;
while (index < chars.length - 1) { // -1 because we're looking for two-char-long things
ch1 = chars[index];
if ((int)ch1 == 0xD83C) {
ch2 = chars[index+1];
if ((int)ch2 >= 0xDF00 && (int)ch2 <= 0xDFFF) {
System.out.println("Found emoji at index " + index);
index += 2;
continue;
}
}
else if ((int)ch1 == 0xD83D) {
ch2 = chars[index+1];
if ((int)ch2 >= 0xDC00 && (int)ch2 <= 0xDDFF) {
System.out.println("Found emoji at index " + index);
index += 2;
continue;
}
}
++index;
}
Obviously that's just debug-level code, but it does the job. (In your given string, with its emoji, of course it won't find anything as they're outside the range. But if you change the upper bound on the second pair to 0xDEFF
instead of 0xDDFF
, it will. No idea if that would also include non-emojis, though.)
Source of my program to find out what the surrogate ranges were:
public class FindRanges {
public static void main(String[] args) {
char last0 = '\0';
char last1 = '\0';
for (int x = 0x1F300; x <= 0x1F5FF; ++x) {
char[] chars = new StringBuilder().appendCodePoint(x).toString().toCharArray();
if (chars[0] != last0) {
if (last0 != '\0') {
System.out.println("-\\u" + Integer.toHexString((int)last1).toUpperCase());
}
System.out.print("\\u" + Integer.toHexString((int)chars[0]).toUpperCase() + " \\u" + Integer.toHexString((int)chars[1]).toUpperCase());
last0 = chars[0];
}
last1 = chars[1];
}
if (last0 != '\0') {
System.out.println("-\\u" + Integer.toHexString((int)last1).toUpperCase());
}
}
}
Output:
\uD83C \uDF00-\uDFFF \uD83D \uDC00-\uDDFF
Solution 3:
Had a similar problem. The following served me well and matches surrogate pairs
public class SplitByUnicode {
public static void main(String[] argv) throws Exception {
String string = "Thats a nice joke πππ π";
System.out.println("Original String:"+string);
String regexPattern = "[\uD83C-\uDBFF\uDC00-\uDFFF]+";
byte[] utf8 = string.getBytes("UTF-8");
String string1 = new String(utf8, "UTF-8");
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher = pattern.matcher(string1);
List<String> matchList = new ArrayList<String>();
while (matcher.find()) {
matchList.add(matcher.group());
}
for(int i=0;i<matchList.size();i++){
System.out.println(i+":"+matchList.get(i));
}
}
}
Output is:
Original String:Thats a nice joke πππ π
0:πππ
1:π
Found the regex from https://stackoverflow.com/a/24071599/915972