本文介绍了正则表达式图释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将数据分成文件ID.我试图遍历每个fileid的数据,并搜索正则表达式定义的表情符号:(
和:)
.如果找到了图释,我需要保留以下信息:a)找到了图释b)在此Fileid中.当我运行这段脚本并打印图释字典时,我得到的值是0.这怎么可能?我是初学者.
I have data split into fileids. I am trying to go through the data per fileid and search for emoticons :(
and :)
as defined by the regex. If an emoticon is found I need to retain the information a) the emoticon was found b) in this fileid. When I run this piece of script and print the emoticon dictionary I get 0 as a value. How is this possible? I am a beginner.
emoticon = 0
for fileid in corpus.fileids():
m = re.search('^(:\(|:\))+$', fileid)
if m is not None:
emoticon +=1
推荐答案
在我看来,您的正则表达式正在运行,并且m
的确不应该是None
.
It looks to me like your regex is working, and that m
should indeed not be None
.
>>> re.search('^(:\(|:\))+$', ':)').group()
':)'
>>> re.search('^(:\(|:\))+$', ':)').group()
':)'
>>> re.search('^(:\(|:\))+$', ':):(').group()
':):('
>>> re.search('^(:\(|:\))+$', ':)?:(').group()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
但是,有些事情对我来说值得怀疑.
However, a few things are questionable to me.
- 这只会匹配100%表情符号的字符串
- fileid确实是您要搜索的内容吗?
这篇关于正则表达式图释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!