当前位置:   article > 正文

【学习python】re 正则表达式匹配特定词性的conll,提取句子主干(主谓宾)_re正则表达式匹配特定词性

re正则表达式匹配特定词性

------------------找寻特定内容-------------------------------

>>> # -*- coding: utf-8 -*-

>>> import re
>>> pattern = re.compile(u'[白蓝绿黄][A-Z][A-Z0-9]{5}')
>>> match = pattern.match(u'白A1B2C3')
>>> if match:
...   print('OK')
... else:
...   print('NO')
...
OK

>>>

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

>>> pattern = re.compile(u'[白蓝绿黄]?\d')
>>> match = pattern.match(u'白1')
>>> if match:
...   print('OK')
... else:
...   print('NO')
...
OK
>>> match = pattern.match(u'白')
>>> if match:
...   print('OK')
... else:
...   print('NO')
...
NO

>>>

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

>>> f = open('E:/abc.txt').read()
>>> import re
>>> pattern = re.compile(r'\S')
>>> match = patter.match(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'patter' is not defined
>>> match = pattern.match(f)
>>> print(match)
<_sre.SRE_Match object; span=(0, 1), match='1'>

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

>>> print(match.group())
1

>>>

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

>>> pattern = re.compile(r'\w')
>>> f = open('E:/abc.txt').read()
>>> match = pattern.match(f)
>>> print(match)
<_sre.SRE_Match object; span=(0, 1), match='1'>

>>>

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

>>> match = r'\d'
>>> f = open('E:/abc.txt').read()
>>> z = re.search(match,f).group()
>>> print(z)
1

>>>

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

>>> match = r'[\u4e00-\u9fa5]'
>>> f = open('E:/abc.txt').read()
>>> z = re.search(match,f).group()
>>> print(z)

>>>

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

>>> match = r'[\u4e00-\u9fa5]*'
>>> f = open('E:/abc.txt').read()
>>> z = re.search(match,f).group()
>>> print(z)


>>> match = r'[\u4e00-\u9fa5]+'
>>> f = open('E:/abc.txt').read()
>>> z = re.search(match,f).group()
>>> print(z)
徐先生

>&g

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/708058
推荐阅读
相关标签
  

闽ICP备14008679号