>>> import re
>>> end = re.compile(r'[u4e00-\u9fa5].$')
>>> start = re.compile(r'[u4e00-\u9fa5].')
>>> with open('E:/切图.txt','r')as f:
... for line in f:
... s = start.search(line)
... e = end.search(line)
... print(s,e)
...
<_sre.SRE_Match object; span=(0, 2), match='切图'> <_sre.SRE_Match object; span=(0, 2), match='切图'>
None None
<_sre.SRE_Match object; span=(1, 3), match='广东'> <_sre.SRE_Match object; span=(14, 16), match='协会'>
None None
<_sre.SRE_Match object; span=(36, 38), match='粤教'> <_sre.SRE_Match object; span=(47, 49), match='1号'>
None None
---------------------------------------------------------------------------
报错:
>>> with open('E:/切图.txt','r')as f:
... for line in f:
... s = start.search(line)
... e = end.search(line)
... print(s.group())
... print(e.group())
...
切图
切图
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
-----------------------------------------------
参考:https://blog.csdn.net/love_dl_forever/article/details/78857030 跳过异常继续运行
https://zhidao.baidu.com/question/618075492622487932.html
>>> with open('E:/切图.txt','r')as f:
... for line in f:
... s = start.search(line)
... e = end.search(line)
... try:
... print(s.group())
... print(e.group())
... except:
... print('wrong')
...
切图
切图
wrong
广东
协会
wrong
粤教
1号
wrong
广东
----------------------------------------------------------------------
>>> end = re.compile(r'[u4e00-\u9fa5]..$')
>>> start = re.compile(r'[u4e00-\u9fa5].')
>>> with open('E:/切图.txt','r')as f:
... for line in f:
... s = start.search(line)
... e = end.search(line)
... try:
... print(s.group())
... print(e.group())
... except:
... print('wrong')
...
切图
wrong
wrong
广东
家协会
wrong
粤教
61号
wrong
广东
的通知
wrong
各普
>>> with open('E:/序言.txt','r')as f:
... for line in f:
... s = start.search(line)
... e = end.search(line)
... try:
... print(s.group()+e.group())
... except:
... print(' ')
...
美国版序言
历史。
这种要性;
很少子了。
企图时代。
最后分的。
本书授过。
正如过我。
伯特·罗素
>>>