1. Match() 和 Search() 的区别:
match试图从字符串起始部分匹配,search不但会搜索字符中字符串中的第一次出现的位置,而且严格的从左往右搜索
m = re.match('foo', 'seafood') #匹配失败
if m is not None:
print(m.group())
m = re.search('foo', 'seafood')
if m is not None:
print(m.group()) #foo
2. 创建字符集([ ])
bt = '[cr][23][dp][o2]'
m = re.match(bt, 'c3po')
if m is not None:
print(m.group()) ##c3po
3. 匹配多个字符串 (|)
bt = 'bat|bet|bit'
m = re.match(bt, 'bat')
if m is not None:
print(m.group()) # bat
4. 分组
m = re.match('(\w\w\w)-(\d\d\d)', 'abc-123')
if m is not None:
print(m.group()) # abc-123
print(m.group(1)) # abc
print(m.group(2)) # 123
print(m.groups()) # ('abc', '123')
5. findall()
m = re.findall('car', 'carry the barcardi to the car')
if m is not None:
print(m) # ['car', 'car', 'car']