正则表达式排除 [英] regexp exclusion

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

问题描述

我有正则表达式将表情符号更改为图像。这是

I have regexp to change smileys to images. Here it is

(?:(?![0]:\)|:\)\)|:-\)\)))(:\)|:-\))

关键是在更改:)和:-)
时不更改0 :)和:))和:-))它与:))和:-))很好用,但是在某种程度上仍然可以抓取:) 0:)

The point is not to change 0:) and :)) and :-)) while changing :) and :-) It works pretty well with :)) and :-)) but somehow still grabs :) in 0:)

我的错误在哪里?

推荐答案

匹配:):-),但是它们不能以 0 <开头/ code>或后跟另一个?然后就是这种模式:

So you want to match :) and :-), but they must not be preceded by 0 or followed by another )? Then this is the pattern:

(?<!0):-?\)(?!\))

基本上是

(?<!0) : negative lookbehind; must not be preceded by 0
:-?\)  : smiley with optional nose
(?!\)) : negative lookforward; must not be followed by )

示例:

$ echo ':) :-) ok 0:) :)) :-)) 0:-)) 0:-) : )' | \
> perl -lne'print $1 while /(?<!0)(:-?\))(?!\))/g'
:)
:-)

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

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