正则表达式元字符说明
. 匹配除换行符以外的任意字符
^ 匹配字符串的开始
$ 匹配字符串的结束
[] 用来匹配一个指定的字符类别
? 对于前一个字符字符重复0次到1次
* 对于前一个字符重复0次到无穷次
{} 对于前一个字符重复m次
{m,n} 对前一个字符重复为m到n次
\d 匹配数字,相当于[0-9]
\D 匹配任何非数字字符,相当于[^0-9]
\s 匹配任意的空白符,相当于[ fv]
\S 匹配任何非空白字符,相当于[^ fv]
\w 匹配任何字母数字字符,相当于[a-zA-Z0-9_]
\W 匹配任何非字母数字字符,相当于[^a-zA-Z0-9_]
\b 匹配单词的开始或结束
模块函数说明即举例
re.compile 将正则表达式编译成pattern对象
compile(pattern, flags=0)
第一个参数:规则
第二个参数:标志位
re.match 只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None
match(pattern, string, flags=0)
第一个参数:规则
第二个参数:表示要匹配的字符串
第三个参数:标致位,用于控制正则表达式的匹配方式
re.search 匹配整个字符串,直到找到一个匹配
search(pattern, string, flags=0)
第一个参数:规则
第二个参数:表示要匹配的字符串
第三个参数:标致位,用于控制正则表达式的匹配方式
>>> import re
>>> pattern = re.compile(r'linuxeye')
>>> match = pattern.match('linuxeye.com')
>>> print match
>>> print match.group()
linuxeye
>>> m = pattern.match('linuxeye.com') #match匹配开头,没找到
>>> print m
None
>>> m = pattern.search('linuxeye.com') #search匹配整个字符串,直到找到一个匹配
>>> print m
>>> print m.group()
linuxeye
>>> m = re.match(r'linuxeye','linuxeye.com') #不用re.compile
>>> print m
>>> print m.group()
linuxeye
>>> m = re.match(r'linuxeye','www.linuxeye.com')
>>> print m
None
re.split 用于来分割字符串
split(pattern, string, maxsplit=0)
第一个参数:规则
第二个参数:字符串
第三个参数:最大分割字符串,默认为0,表示每个匹配项都分割
实例:分割所有的字符串
>>> import re
>>> test_str = "1 2 3 4 5"
>>> re.split(r'\s+',test_str)
['1', '2', '3', '4', '5']
>>> re.split(r'\s+',test_str,2) #分割前2个
['1', '2', '3 4 5']
>>> test_str = "1 . 2. 3 .4 . 5"
>>> re.split(r'\.',test_str)
['1 ', ' 2', ' 3 ', '4 ', ' 5']
>>> re.split(r'\.',test_str,3)
['1 ', ' 2', ' 3 ', '4 . 5']
re.findall 在目标字符串查找符合规则的字符串
findall(pattern, string, flags=0)
第一个参数:规则
第二个参数:目标字符串
但三个参数:后面还可以跟一个规则选择项
返回的结果是一个列表,建中存放的是符合规则的字符串,如果没有符合规则的字符串呗找到,就会返回一个空值
>>> import re
>>> test_mail = ' test03@gmail.net'
>>> mail_re = re.compile(r'\w+@g....\.[a-z]{3}')
>>> re.findall(mail_re,test_mail)
['test01@gmail.com', 'test02@gmail.org', 'test03@gmail.net']
re.sub 以正则表达式为基础的替换工作
sub(pattern, repl, string, count=0)
第一个参数:规则
第二个参数:替换后的字符串
第三个参数:字符串
第四个参数:替换个数。默认为0,表示每个匹配项都替换
>>> test = 'linuxeye.com linuxeye.com'
>>> test_re = re.compile(r'\.')
>>> re.sub(test_re,'--',test)
'blog--linuxeye--com linuxeye--com'
>>> re.sub(test_re,'--',test,1)
'blog--linuxeye.com linuxeye.com'
Sun Oct 20 17:46:09 CST 2013