我有一个正则表达式,可以将表情符号转换为图片。下面是它的代码:
(?:(?![0]:\)|:\)\)|:-\)\)))(:\)|:-\))
重点不是更改0:)和:))以及:-))而是更改:)和:-) 它可以很好地处理:))和:-)),但某种方式仍然捕获了0:)中的:) 我的错误在哪里?
你希望匹配:)
和:-)
,但它们不能以0
为前导或后跟另一个)
?那么这就是模式:
(?<!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'
:)
:-)