#!/usr/bin/python
#-*-coding: utf-8-*-
import re
def info(m):
if m is not None:print m.group()
print 'match 和 search 的区别'
m = re.search('foo', 'seafood')
info(m)
m = re.match('foo', 'seafood')
info(m)
#'search 和 match最大的区别就是:match 是其起始处匹配模式,字符串的第一个字符必须满足匹配模式第一个字符,以此类推;search 则搜索整个字符串,而非第一个字符串'
print '匹配多个字符串 |'
bt = 'bat|bet|bit'
m = re.match(bt, 'bat')
info(m)
m = re.match(bt, 'blt')
info(m)
m = re.match(bt, 'He bit me!')
info(m)
m = re.search(bt, 'He bit me!')
info(m)
print '匹配任意单个字符(.)'
anyend = '.end'
m = re.match(anyend, 'bend')
info(m)
m = re.match(anyend, 'end')
info(m)
m = re.match(anyend, '\nend')
info(m)
m = re.search(anyend, 'The end.')
info(m)
print '创建字符集合([])'
m = re.match('[cr][23][dp][o2]', 'c3po')
info(m)
m = re.match('kevin|gary', '1gary1')
info(m)
m = re.search('kevin|gary', '1gary1')
info(m)
print '重复特殊字符和自组'
patt = '\w+@(\w+\.)?\w+\.com'
m = re.match(patt, 'nobody@xxx.com')
info(m)
m = re.match(patt, 'nobody@www.xxx.com')
info(m)
m = re.match('(\w\w\w)-(\d\d\d)', 'abc-123')
print m.group()
print m.group(1)
print m.group(2)
print m.groups()
m = re.match('((a)(b))', 'ab')
print m.groups()
print m.group()
print m.group(0)
print m.group(1)
print m.group(2)
print m.group(3)
print '开头,结尾,边界的匹配'
m = re.search('^The', 'The end.')
info(m)
m = re.search('^the', 'end.the')
info(m)
m = re.search(r'\bthe', 'bite the dog')
info(m)
m = re.search(r'\bthe', 'bitethe dog')
info(m)
m = re.search(r'\Bthe', 'bitthe dog')
info(m)
print 'findall()找到每一个匹配的部分'
print re.findall('car', 'car')
print re.findall('car', 'the car is good, so car他')
print 'sub()和subn()搜索和替换'
print re.sub('X', 'Mr. Simth', 'attn: X\nDear X,\n')
print re.subn('X', 'Mr. Simth', 'attn: X\nDear X,\n')
print re.sub('[ab]', 'X', 'gary,bing')
print '用split()分割(分割模式)'
print re.split(':', 'str1:str2:str3')
print '贪婪模式'
patt = '\d+-\d+-\d+'
data = 'Thu Feb 15 17:46:04 2007::uzifzf@dpyivihw.gov::1171590364-6-8'
print re.search(patt, data).group()
patt = '.+?(\d+-\d+-\d+)'
m = re.search(patt, data)
print m.group()
print m.group(0)
print m.group(1)
#-*-coding: utf-8-*-
import re
def info(m):
if m is not None:print m.group()
print 'match 和 search 的区别'
m = re.search('foo', 'seafood')
info(m)
m = re.match('foo', 'seafood')
info(m)
#'search 和 match最大的区别就是:match 是其起始处匹配模式,字符串的第一个字符必须满足匹配模式第一个字符,以此类推;search 则搜索整个字符串,而非第一个字符串'
print '匹配多个字符串 |'
bt = 'bat|bet|bit'
m = re.match(bt, 'bat')
info(m)
m = re.match(bt, 'blt')
info(m)
m = re.match(bt, 'He bit me!')
info(m)
m = re.search(bt, 'He bit me!')
info(m)
print '匹配任意单个字符(.)'
anyend = '.end'
m = re.match(anyend, 'bend')
info(m)
m = re.match(anyend, 'end')
info(m)
m = re.match(anyend, '\nend')
info(m)
m = re.search(anyend, 'The end.')
info(m)
print '创建字符集合([])'
m = re.match('[cr][23][dp][o2]', 'c3po')
info(m)
m = re.match('kevin|gary', '1gary1')
info(m)
m = re.search('kevin|gary', '1gary1')
info(m)
print '重复特殊字符和自组'
patt = '\w+@(\w+\.)?\w+\.com'
m = re.match(patt, 'nobody@xxx.com')
info(m)
m = re.match(patt, 'nobody@www.xxx.com')
info(m)
m = re.match('(\w\w\w)-(\d\d\d)', 'abc-123')
print m.group()
print m.group(1)
print m.group(2)
print m.groups()
m = re.match('((a)(b))', 'ab')
print m.groups()
print m.group()
print m.group(0)
print m.group(1)
print m.group(2)
print m.group(3)
print '开头,结尾,边界的匹配'
m = re.search('^The', 'The end.')
info(m)
m = re.search('^the', 'end.the')
info(m)
m = re.search(r'\bthe', 'bite the dog')
info(m)
m = re.search(r'\bthe', 'bitethe dog')
info(m)
m = re.search(r'\Bthe', 'bitthe dog')
info(m)
print 'findall()找到每一个匹配的部分'
print re.findall('car', 'car')
print re.findall('car', 'the car is good, so car他')
print 'sub()和subn()搜索和替换'
print re.sub('X', 'Mr. Simth', 'attn: X\nDear X,\n')
print re.subn('X', 'Mr. Simth', 'attn: X\nDear X,\n')
print re.sub('[ab]', 'X', 'gary,bing')
print '用split()分割(分割模式)'
print re.split(':', 'str1:str2:str3')
print '贪婪模式'
patt = '\d+-\d+-\d+'
data = 'Thu Feb 15 17:46:04 2007::uzifzf@dpyivihw.gov::1171590364-6-8'
print re.search(patt, data).group()
patt = '.+?(\d+-\d+-\d+)'
m = re.search(patt, data)
print m.group()
print m.group(0)
print m.group(1)