【python】提取文章句子中的,开头与结尾的两个中文字符。

参考:

【python】正则表达式,提取句子开头两个字

    正则表达式在线生成工具

---------------------------------------------------------------------------

>>> end = re.compile(r'[\u4e00-\u9fa5].$')
>>> start = re.compile(r'^[\u4e00-\u9fa5].')
>>> with open('E:/000.txt','r')as f:
...   for line in f:
...     s = start.search(line)
...     e = end.search(line)
...     print(s&e)
...
Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
TypeError: unsupported operand type(s) for &: '_sre.SRE_Match' and '_sre.SRE_Match'
>>> with open('E:/000.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=(3, 5), match='序言'>
None None
None None
None None
None None
<_sre.SRE_Match object; span=(0, 2), match='我的'> None
<_sre.SRE_Match object; span=(0, 2), match='这一'> None
<_sre.SRE_Match object; span=(0, 2), match='我还'> None
<_sre.SRE_Match object; span=(0, 2), match='经院'> None

-----------------------------------------------

两个重要网站,第一个可视化正则表达式

http://tools.jb51.net/regex/javascript

第二个各种不同语言的写法

http://tools.jb51.net/regex/create_reg


-----------------------------------------




参考:https://blog.csdn.net/qq_19741181/article/details/79360473

-----------------------------------------------------------

参考:去掉空白格 http://www.iplaypy.com/sys/s95.html

>>> with open('E:/000.txt','r')as f:
...   for line in f:
...     line.strip()
...     s = start.search(line)
...     e = end.search(line)
...     print(s,e)
...

-----------------------

>>> with open('E:/切图.txt','r')as f:
...   for line in f:
...     l = line.strip()
...     s = start.search(l)
...     e = end.search(l)
...     print(s,e)
...
<_sre.SRE_Match object; span=(0, 2), match='切图'> None
None None
<_sre.SRE_Match object; span=(0, 2), match='广东'> None
None None
<_sre.SRE_Match object; span=(0, 2), match='粤教'> None
None None
<_sre.SRE_Match object; span=(0, 2), match='广东'> None
None None



阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭