赞
踩
------------------找寻特定内容-------------------------------
>>> # -*- coding: utf-8 -*-
>>> import re>>>
-------------------------------------------------------------------
>>> 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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。