我有一些字符串,其中包含各种不同的表情符号/图像/符号。
并非所有字符串都是英文的——其中一些字符串是其他非拉丁语,例如:
▓ railway??
→ Cats and dogs
I'm on 🔥
Apples ⚛
✅ Vi sign
♛ I'm the king ♛
Corée ♦ du Nord ☁ (French)
gjør at både ◄╗ (Norwegian)
Star me ★
Star ⭐ once more
早上好 ♛ (Chinese)
Καλημέρα ✂ (Greek)
another ✓ sign ✓
добрай раніцы ✪ (Belarus)
◄ शुभ प्रभात ◄ (Hindi)
✪ ✰ ❈ ❧ Let's get together ★. We shall meet at 12/10/2018 10:00 AM at Tony's.❉
……还有更多。
我想摆脱所有这些标志/图像,只保留不同语言的字母(和标点符号)。
我尝试使用 EmojiParser 库 清理标志:
String withoutEmojis = EmojiParser.removeAllEmojis(input);
问题是 EmojiParser 无法删除大部分符号。 ♦ 标志是迄今为止我发现的唯一一个被删除的标志。其他标志如 ✪ ❉ ★ ✰ ❈ ❧ ✂ ❋ ⓡ ✿ ♛ 🔥 没有被删除。
有没有办法从输入字符串中删除所有这些符号,只保留 不同语言 的字母和标点符号?
原文由 riorio 发布,翻译遵循 CC BY-SA 4.0 许可协议
与其将某些元素列入黑名单,不如创建一个你希望保留的字符的白名单怎么样?这样您就不必担心每个新的表情符号都会被添加。
所以:
[\\p{L}\\p{M}\\p{N}\\p{P}\\p{Z}\\p{Cf}\\p{Cs}\\s]
is a range representing all numeric (\\p{N}
), letter (\\p{L}
), mark (\\p{M}
), punctuation (\\p{P}
), whitespace/separator (\\p{Z}
), other formatting (\\p{Cf}
) and other characters aboveU+FFFF
in Unicode (\\p{Cs}
) 和换行符 (\\s
) 字符。\\p{L}
具体 包括来自其他字母表的字符,如西里尔字母、拉丁字母、汉字等。^
否定匹配。例子:
如果您需要更多信息,请查看有关正则表达式的 Java 文档。