我希望将:)
/ :))))
...替换为+
,以查看所出现的次数。
e.g:
:) -> +
:))) -> +++
同样是否定的:
:( -> -
:((( -> ---
它也应该匹配左手表情符号:
(: -> +
(((: -> +++
): -> -
))): -> ---
可能吗? :)
答案 0 :(得分:1)
为什么不呢?
>>> def get_emoticon(txt):
return re.sub(r':(([\(\)])+)', lambda m: '-'*len(m.group(1)) if m.group(2) == '(' else '+' * len(m.group(1)), txt)
>>> get_emoticon(':) hi :))) how r you? :((( :(')
'+ hi +++ how r you? --- -'
>>>
更新
>>> def get_emoticon(txt):
def func(match):
if match.group().startswith(':'):
return '-'*len(match.group(1)) if match.group(2) == '(' else '+' * len(match.group(1))
return '+'*len(match.group(3)) if match.group(4) == '(' else '-' * len(match.group(3))
return re.sub(r':(([\(\)])+)|(([\(\)])+):', lambda m: func(m), txt)
>>> get_emoticon(':) (: hi :))) how r you? :((( :(')
'+ + hi +++ how r you? --- -'
>>> get_emoticon(':) (: ))): hi :))) how r you? :((( :(')
'+ + --- hi +++ how r you? --- -'
>>>