以下是学习goole PYTHON教程过程中的一些笔记。
'#
' 后面的是输出,希望你能从中获取你想要的。
Regular Expressions
import re
match = re.search(pat, text) #match object
match = re.search('iig', 'called piiig')
#<_sre,SRE_Match object at 0xf7f7c448>
match.group()#'iig'
match = re.search('igs', 'called piig') #
match.group() #error
def Find(pat, text):
match = re.search(pat,text)
if match: print match.group()
else: print 'not found'
Find('ig', 'called piiig')
#ig
Find('hello', 'called piiig')
#not found
Find('...g', 'called piiig')
#iiig
Find('x...g', 'called piiig')
#not found
Find('..gs', 'called piiig')
#not found
Find('..g', 'called piig much better: xyzg')
#iig
Find('x..g', 'called piiig much better: xyzg')
#zyzg
Find(r'c\.l', 'c.lled piiig much better:xyzgs')
#c.l
Find(r':\w\w\w', 'blah :cat blah blah')
#cat
Find(r'\d\d\d', 'blah :123xxx ')
#123
Find(r'\d\s\d\s\d', '1 2 3')
#1 2 3
Find(r'\d\s+\d\s+\d', '1 2 3')
#1 2 3
Find(r':\w+', 'blah blah :kitten blabh blah')
#:kitten
Find(r':\w+', 'blah blah :kitten123 blabh blah')
#:kitten123
Find(r':\w+', 'blah blah :kitten123& blabh blah')
#:kitten123
Find(r':.+', 'blah blah :kitten123& blabh blah')
#blah blah :kitten123& blabh blah
Find(r':\S+', 'blah blah :kitten123&a=123&yatta blabh blah')
#kitten123&a=123&yatta
#p@gmail
#nick.p@gmail
#<_sre.SRE_Match object at 0xF7F649F8>
m.group()
m.group(1)
#'nick.p'
m.group(2)
#'gmail.com'
#[('nick.p', 'gmail.com'), ('foo', 'bar')]
\w#word char
\d #digit
\s #whitespace
\S #non-whitespace
+ #1 or more
* #0 or more