我有一个用UTF-8编码的字符串.例如:
Thats a nice joke
Run Code Online (Sandbox Code Playgroud)
我必须提取句子中的所有表情符号.表情符号可以是任何表情符号
当在终端使用命令查看此句子时,less text.txt它被视为:
Thats a nice joke <U+1F606><U+1F606><U+1F606> <U+1F61B>
Run Code Online (Sandbox Code Playgroud)
这是表情符号的相应UTF代码.emojis的所有代码都可以在emojitracker找到.
为了找到所有的出现,我使用了正则表达式模式,(<U\+\w+?>)但它不适用于UTF-8编码的字符串.
以下是我的代码:
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));
}
Run Code Online (Sandbox Code Playgroud)
这个pdf说Range: 1F300–1F5FF for Miscellaneous Symbols and Pictographs.所以我想捕捉这个范围内的任何角色.
我的问题是从字符串中删除表情符号,但不使用正则表达式从字符串中删除CJK(中文,日文,韩文)字符.我试着用这个正则表达式:
REGEX = /[^\u1F600-\u1F6FF\s]/i
Run Code Online (Sandbox Code Playgroud)
这个正则表达式工作正常,除了它还检测我需要这些字符的中文,日文和韩文字符.不知道如何解决这个问题?
众所周知,表情符号最多可编码为3或4个字节,因此它可能会占用我的字符串中的2个符号.例如'wew'.length = 7我想在我的文本中找到这些符号,并将它们替换为依赖于其代码的值.读SO,我带着unicode插件来到XRegExp库,但还没找到如何使它工作的方法.
var str = 'wew';// \u1F601 symbol
var reg = XRegExp('[\u1F601-\u1F64F]', 'g'); // /[?1-?F]/g -doesn't make a lot of sense
//var reg = XRegExp('[\uD83D\uDE01-\uD83D\uDE4F]', 'g'); //Range out of order in character class
//var reg = XRegExp('\\p{L}', 'g'); //doesn't match my symbols
console.log(XRegExp.replace(str, reg, function(match){
return encodeURIComponent(match);// here I want to have smth like that %F0%9F%98%84 to be able to map anything I want to this value and replace to it
}));
Run Code Online (Sandbox Code Playgroud)
我真的不想强行查找我的范围内的字符序列.有人可以帮我找到用regexp做的方法.
EDITED刚想 出了一个枚举所有表情符号的想法.比野蛮力更好,但仍然在寻找更好的主意
var …Run Code Online (Sandbox Code Playgroud) 注意:这个问题在不支持包含的表情符号的系统上看起来很奇怪.
这是如何从字符串中删除表情符号的后续问题.
我想构建一个正则表达式,匹配可以在Mac OS X/iOS中输入的所有表情符号.
明显的Unicode块涵盖了大多数,但不是所有这些表情符号:
维基百科提供了OS X Mountain Lion和iOS 6上Apple Color Emoji中可用的所有符号的编译列表,这看起来是一个很好的起点:(稍微更新)
people = '????????'
nature = '????????'
objects = '?????????????'
places = '?????????'
symbols = '1??2??3??4??5??6??7??8??9??0??#??????????????????????????????????????????????????????????????????????????????????????©?®?™??????????????????????????????????????????????'
emoji = people + nature + objects + places + symbols # all emoji combined
Run Code Online (Sandbox Code Playgroud)
大多数字符都有一个代码点,转换它们很容易:
但有些字符是"使用 …
我试图从unicode tweet文本中删除表情符号,并使用python 2.7打印出结果
myre = re.compile(u'[\u1F300-\u1F5FF\u1F600-\u1F64F\u1F680-\u1F6FF\u2600-\u26FF\u2700-\u27BF]+',re.UNICODE)
print myre.sub('', text)
Run Code Online (Sandbox Code Playgroud)
但似乎几乎所有的字符都从文本中删除.我从其他帖子中查了几个答案,不幸的是,这些都没有在这里工作.我在re.compile()中做错了吗?
这是一个删除所有字符的示例输出:
“ ' //./” ! # # # …
Run Code Online (Sandbox Code Playgroud)