如何将表情符号与正则表达式匹配? [英] How to match emoticons with regular expressions?

查看:102
本文介绍了如何将表情符号与正则表达式匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要捕捉像

:)
:P
:-P
=)
:D
;)

等等,以及一般文本.这是我当前的正则表达式:

And so on, along with general text. This is my current regex:

\b[0-9A-Za-z'\&\-\./()=:;]+\b

然而,它不匹配 ()=:;因为某些原因.我错过了什么吗?

However, it doesn't match ()=:; for some reason. Am I missing something?

根据 Mark 的反馈,这里有一个我需要解析的示例:

Based on Mark's feedback here is an example that I need to parse:

hi =as.) friend :) haha yay! ;) =) test test) R&R I.O.U. 24/7

这应该提取:

hi
friend
:)
haha
yay
;)
=)
test
test
R&R
I.O.U.
24/7

我无法使用所提出的任何解决方案使其正常工作.

I'm having trouble getting this to work using any of the solutions proposed.

推荐答案

这是一个捕获单词后跟上述示例的示例.它在单独的捕获组中捕获单个单词和随后的表情符号.Rubular 链接.

This is an example that captures a word followed by the above examples. It captures a single word and a following emoticon in separate capture groups. The Rubular link.

\s(\w+)\s((?::|;|=)(?:-)?(?:\)|D|P))

编辑根据编辑和给定的示例,这可能是我们想要的.它定义了两个捕获组,一个用于一般文本,另一个用于表情符号.这是 Rubular 链接.

Edit Based on the edits and the given example, this might be what is desired. It defines two capture groups, one for the general text and one for an emoticon. Here is the Rubular link.

([0-9A-Za-z'\&\-\.\/\(\)=:;]+)|((?::|;|=)(?:-)?(?:\)|D|P))

这篇关于如何将表情符号与正则表达式匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆