Skip to content

Instantly share code, notes, and snippets.

@cmkilger
Last active July 1, 2023 07:17
Show Gist options
  • Save cmkilger/b8f7dba3e76244a84e7e to your computer and use it in GitHub Desktop.
Save cmkilger/b8f7dba3e76244a84e7e to your computer and use it in GitHub Desktop.
Detects if a string is made up of just emoji (and space).
import java.util.regex.*;
class EmojiDetector {
private static Pattern r = Pattern.compile("^[\\s\n\r]*(?:(?:[\u00a9\u00ae\u203c\u2049\u2122\u2139\u2194-\u2199\u21a9-\u21aa\u231a-\u231b\u2328\u23cf\u23e9-\u23f3\u23f8-\u23fa\u24c2\u25aa-\u25ab\u25b6\u25c0\u25fb-\u25fe\u2600-\u2604\u260e\u2611\u2614-\u2615\u2618\u261d\u2620\u2622-\u2623\u2626\u262a\u262e-\u262f\u2638-\u263a\u2648-\u2653\u2660\u2663\u2665-\u2666\u2668\u267b\u267f\u2692-\u2694\u2696-\u2697\u2699\u269b-\u269c\u26a0-\u26a1\u26aa-\u26ab\u26b0-\u26b1\u26bd-\u26be\u26c4-\u26c5\u26c8\u26ce-\u26cf\u26d1\u26d3-\u26d4\u26e9-\u26ea\u26f0-\u26f5\u26f7-\u26fa\u26fd\u2702\u2705\u2708-\u270d\u270f\u2712\u2714\u2716\u271d\u2721\u2728\u2733-\u2734\u2744\u2747\u274c\u274e\u2753-\u2755\u2757\u2763-\u2764\u2795-\u2797\u27a1\u27b0\u27bf\u2934-\u2935\u2b05-\u2b07\u2b1b-\u2b1c\u2b50\u2b55\u3030\u303d\u3297\u3299\ud83c\udc04\ud83c\udccf\ud83c\udd70-\ud83c\udd71\ud83c\udd7e-\ud83c\udd7f\ud83c\udd8e\ud83c\udd91-\ud83c\udd9a\ud83c\ude01-\ud83c\ude02\ud83c\ude1a\ud83c\ude2f\ud83c\ude32-\ud83c\ude3a\ud83c\ude50-\ud83c\ude51\u200d\ud83c\udf00-\ud83d\uddff\ud83d\ude00-\ud83d\ude4f\ud83d\ude80-\ud83d\udeff\ud83e\udd00-\ud83e\uddff\udb40\udc20-\udb40\udc7f]|\u200d[\u2640\u2642]|[\ud83c\udde6-\ud83c\uddff]{2}|.[\u20e0\u20e3\ufe0f]+)+[\\s\n\r]*)+$");
private static Boolean isEmojiOnly(String string) {
return r.matcher(string).find();
}
private static void test(String string, Boolean isEmoji) {
if (isEmojiOnly(string) != isEmoji) {
System.out.println(string + " should" + (isEmoji ? "" : " not") + " be emoji only");
}
}
public static void main(String[] args) {
test("Hello ๐Ÿ˜ฒ", false);
test("-", false);
test("+", false);
test("$", false);
test("๐Ÿ‘ด๐Ÿป", true); // Requires Unicode 8.0 support for skin tones
test(" ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ง ", true);
test(" 0๏ธโƒฃ โค๏ธ", true);
test("๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ง โค๏ธ", true);
test(" 0๏ธโƒฃ โค๏ธ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ง ", true);
test("0๏ธโƒฃ\nโค๏ธ", true);
test("ยฉยฎ๐Ÿ”๐Ÿ”›๐Ÿ”™๐Ÿ’ฑโž•โž–โž—ยฎยฉโ„ข๐Ÿ”œ๐Ÿ”ด๐Ÿ”ตโšซ๏ธโšช๏ธ๐Ÿ”น๐Ÿ”˜๐Ÿ”ธโ–ช๏ธโ–ซ๏ธโ—ป๏ธโ—พ๏ธ๐Ÿ”‰๐Ÿ”‡โ™ฆ๏ธโ™ฅ๏ธโ™ฃ๏ธโ™ ๏ธ๐Ÿ€„๏ธ๐ŸŽด๐Ÿ‘โ€๐Ÿ—จ๐Ÿ’ญ๐Ÿ—ฏ๐Ÿ•”๐Ÿ•“๐Ÿ•–๐Ÿ•–๐Ÿ•œ๐Ÿ•ฅ๐Ÿ’ฌ๐Ÿ•Žโ˜ชโ˜ช๐Ÿ”โ˜‘๏ธ#๏ธโƒฃ#๏ธโƒฃโคต๏ธ*๏ธโƒฃโ„น๏ธโ†ฉ๏ธโ†ช๏ธโ†™๏ธโ†˜๏ธโžก๏ธ๐Ÿ”‚โ—€๏ธ๐Ÿ”ผโธโฏ4๏ธโƒฃ๐Ÿ†“๐Ÿ†–๐Ÿ“ถ๐Ÿ†’๐Ÿ›„๐Ÿง๐ŸŒ€๐Ÿ’ โœ…ใ€ฝ๏ธ๐Ÿ’ฏโ‰๏ธโ•๐Ÿšณ๐Ÿšฏ๐Ÿšท๐ŸšซโŒโญ•๏ธ๐Ÿ’ขโ›”๏ธ๐Ÿ…ฑใŠ™๏ธโœด๏ธ๐Ÿ†š๐Ÿ’˜๐Ÿ’•๐Ÿ’š๐Ÿ”Ž๐Ÿ“๐Ÿ”“๐Ÿšฉ๐ŸŽ๐ŸŽ€โ›ฑ๐Ÿ›๐Ÿ—ฟ๐Ÿ”‘๐Ÿท๐Ÿ”ฌ๐Ÿบ๐Ÿ›กโš”๐Ÿ”ซ๐Ÿ› ๐Ÿ› ๐Ÿ’Ž๐Ÿ’ด๐Ÿ•ฏ๐Ÿ“กโฒ๐Ÿ“ป๐Ÿ“ž๐Ÿ“ธ๐Ÿ’พ๐Ÿ–ฑ๐Ÿ“ฒ๐Ÿ•๐Ÿฏ๐ŸŽ‡๐Ÿ™๐Ÿœ๐Ÿž๐ŸŽข๐Ÿšฅ๐Ÿšฆ๐Ÿ’บ๐Ÿšค๐Ÿš‡๐Ÿš…๐ŸšŸ๐Ÿš๐Ÿšœ๐Ÿš‘๐Ÿš™๐ŸŽท๐ŸŽบ๐Ÿšด๐Ÿพ๐ŸŽฑ๐Ÿ‰๐Ÿ€๐Ÿถ๐Ÿต๐Ÿฟ๐ŸŽ‚๐Ÿก๐Ÿ›๐ŸŒญ๐Ÿ ๐Ÿ๐Ÿ‡โ›„๏ธโ˜ƒ๐ŸŒจโ˜€๏ธโ˜„โญ๏ธ๐ŸŒ๐ŸŒš๐ŸŒš๐ŸŒ๐ŸŒ‘๐ŸŒ’๐ŸŒ•๐ŸŒบ๐Ÿš๐Ÿ•ธ๐Ÿƒ๐ŸŒด๐Ÿ‘๐Ÿ„๐Ÿ„๐ŸŒ๐Ÿฆ๐Ÿ—๐Ÿฝ๐Ÿญ๐Ÿ’๐Ÿ•ถ๐Ÿ‘“๐Ÿ‘ ๐Ÿ‘ž๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿ‘ฉโ€โค๏ธโ€๐Ÿ‘ฉ๐Ÿ™Ž๐Ÿผ๐Ÿ’‚๐Ÿฝ๐Ÿ‘ฅ๐Ÿ‘‡๐Ÿป๐Ÿ‘๐Ÿป๐Ÿ˜ฟ๐Ÿค•๐Ÿ˜ฒ๐Ÿ˜ช๐Ÿ™๐Ÿ˜Ÿ๐Ÿ™„๐Ÿ˜˜๐Ÿ˜๐Ÿ‘๐ŸŒด๐Ÿ‘“๐Ÿ‘๐Ÿป๐Ÿ˜ฒ๐Ÿ˜˜", true);
test("ใŽ", false);
test("ใท", false);
test("ไท‘", false);
test("ไทซ", false);
test("ไทฝ", false);
test("๊’ž", false);
}
}
@cmkilger
Copy link
Author

cmkilger commented Jul 27, 2016

\p{So}\p{Sk}\p{M} was matching a lot of things which would not be considered emoji. Now using specific code points and ranges.

@cmkilger
Copy link
Author

Support gender.

@cmkilger
Copy link
Author

Added support for flag tags.

@ShahoodulHassan
Copy link

How can we then find out the number of emojis if isEmojiOnly returns true?

@crazytosser00
Copy link

Thanks, dude. You made great work with searching unicode blocks for emojis and making regex pattern.

@cmkilger
Copy link
Author

Be aware that more emoji have been added to Unicode since I made this. It could use an update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment