正则表达式匹配图释 [英] Regex matching emoticons

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

问题描述

我们正在一个项目中,我们希望用户能够使用表情符号语法(例如:smile::heart::confused::stuck_out_tongue:)和普通表情符号(例如:)<3:/:p)

We are working on a project where we want users to be able to use both emoji syntax (like :smile:, :heart:, :confused:,:stuck_out_tongue:) as well as normal emoticons (like :), <3, :/, :p)

我在使用图释语法时遇到麻烦,因为有时这些字符序列会出现在以下位置:

I'm having trouble with the emoticon syntax because sometimes those character sequences will occur in:

  • 正常字符串或URL- http ://example.com
  • 在表情符号语法内- :p encil:
  • normal strings or URL's - http://example.com
  • within the emoji syntax - :pencil:

如何找到这些表情符号字符序列,但当其他字符靠近它们时找不到?

How can I find these emoticon character sequences but not when other characters are near them?

我用于所有表情符号的整个正则表达式非常庞大,因此,这是一个精简的版本:

The entire regex I'm using for all the emoticons is huge, so here's a trimed down version:

(\:\)|\:\(|<3|\:\/|\:-\/|\:\||\:p)

您可以在此处进行演示: http://regexr.com/3a8o5

You can play with a demo of it in action here: http://regexr.com/3a8o5

推荐答案

首先匹配表情符号(以:pencil:示例为例),然后检查终止的空格或换行符:

Match emoji first (to take care of the :pencil: example) and then check for a terminating whitespace or newline:

(\:\w+\:|\<[\/\\]?3|[\(\)\\\D|\*\$][\-\^]?[\:\;\=]|[\:\;\=B8][\-\^]?[3DOPp\@\$\*\\\)\(\/\|])(?=\s|[\!\.\?]|$)

此正则表达式与以下(首选表情符号)匹配,返回匹配组1中的匹配项:

This regex matches the following (preferring emoji) returning the match in matching group 1:

:( :) :P :p :O :3 :| :/ :\ :$ :* :@
:-( :-) :-P :-p :-O :-3 :-| :-/ :-\ :-$ :-* :-@
:^( :^) :^P :^p :^O :^3 :^| :^/ :^\ :^$ :^* :^@
): (: $: *:
)-: (-: $-: *-:
)^: (^: $^: *^:
<3 </3 <\3
:smile: :hug: :pencil:

除空白外,它还支持将终端标点符号用作分隔符.

It also supports terminal punctuation as a delimiter in addition to white space.

您可以在此处查看更多详细信息并对其进行测试: https://regex101.com/r/aM3cU7/4

You can see more details and test it here: https://regex101.com/r/aM3cU7/4

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

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