正则表达式是用于处理字符串的强大工具,拥有自己独特的语法和自己独立的处理机制,在提供了有正则表达式的语言里,正则表达式的语法是一样的,只是不同的语言所支持的语法数量不同。
正则表达式实现的流程为:
下面具体介绍正则表达式
在python中,re模块是用于对正则表达式的操作
字符:
. 匹配除换行符以外的任意字符
\w 匹配字母或数字或下划线或汉子
\d 匹配数字
\s 匹配任意的空白符
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束
次数:
* 重复零次或更多次
+ 重复一次或更多次
? 重复零次或一次
{n}重复n次
{n,}至少重复n次
{n,m}重复n到m次
re的几个常用方法
1、match(pattern,string,flags=0)也就是从起始位置开始根据模型去字符串中匹配指定类容(匹配单个)
正则表达式
匹配的字符串
标志位
import re obj = re.match(‘\d+‘,‘111122ads111‘) print obj.group()
flag
I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case L = LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale U = UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode locale M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline S = DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
2、search(pattern,string,flags=0)
根据模型去字符串中匹配指定内容,匹配单个
import re obj = re.search(‘\d+‘,‘ddds222dddd1111‘) print obj.group()
3、group(self,*args)返回被re匹配的字符串
import re a = "123abc456" print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group()#123abc456 print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(0)#123abc456 print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(1)#123 print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(2)#abc print re.search("([0-9]*)([a-z]*)([0-9]*)", a).groups()#(‘123‘, ‘abc‘, ‘456‘)
4、findall(pattern,string,flags=0)
匹配字符串中所有符合匹配条件的内容
import re a = "123abc456" obj = re.findall("\d+",a) print obj #[‘123‘, ‘456‘]
5、sub(pattern, repl, string, count=0, flags=0)
用于替换匹配的字符串
import re a = "123abc456" obj = re.sub("\d+",‘yy‘,a) print obj #yyabcyy
6、split(pattern, string, maxsplit=0, flags=0) 根据指定内容分组
import re content = "‘1 - 2 * ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )‘" new_content = re.split(‘\*‘, content) # new_content = re.split(‘\*‘, content, 1) print new_content #["‘1 - 2 ", ‘ ((60-30+1‘, ‘(9-2‘, ‘5/3+7/3‘, ‘99/4‘, ‘2998+10‘, ‘568/14))-(-4‘, ‘3)/(16-3‘, "2) )‘"]
对正则表达式总结
1、数字:^[0-9]* [0-9]*$
import re str = ‘123ff446623‘ print re.findall(‘(^[0-9]*)ff([0-9]*$)‘,str) #匹配符合要求的所有 print re.search(‘(^[0-9]*)ff[0-9]*$‘,str).group()#search 只匹配一个,即第一个匹配的 print re.search(‘\d+‘,str).group() print re.search(‘^[0-9]*‘,str).group() print re.search(‘\d+$‘,str).group() print re.search(‘[0-9]*$‘,str).group() print re.match(‘\d+‘,str).group() print re.match(‘^[0-9]*‘,str).group()
2、n位的数字 ^\d{n}$
import re str = ‘123ff446623‘ print re.match(‘\d{2}‘,str).group() print re.search(‘\d{2}‘,str).group() print re.search(‘^\d{2}‘,str).group() print re.search(‘\d{2}$‘,str).group()
3、至少n位的数字:^\d{n,}$
import re str = ‘123ff446623‘ print re.match(‘\d{2,}‘,str).group() print re.search(‘\d{2,}‘,str).group() print re.search(‘^\d{2,}‘,str).group() print re.search(‘\d{2,}$‘,str).group()
4、m-n位的数字:^\d{n,m}$
import re str = ‘123ff446623‘ print re.match(‘\d{2,3}‘,str).group() print re.search(‘\d{2,3}‘,str).group() print re.search(‘^\d{2,3}‘,str).group() print re.search(‘\d{2,6}$‘,str).group()
5、零和非0开头的数字:^(0|[1-9][0-9]*)$
import re str = ‘0123ff446623‘ print re.search(‘^(0[1-9]*|[1-9][0-9]*)‘,str).group() #0123 str1 = ‘123ff446623‘ print re.search(‘^(0|[1-9][0-9]*)‘,str1).group()#123
6、非零开头的最多带两位小数的数字:^([1-9][0-9]*)+(.[0-9]{1,2})?$
import re str = ‘21.33efsdfdf12.23‘ print re.search(‘^([1-9][0-9]*)+(.[0-9]{1,2})?‘,str).group() print re.search(‘([1-9][0-9]*)+(.[0-9]{1,2})?$‘,str).group() print re.findall(‘^([1-9][0-9].[0-9]{1,2})?‘,str) print re.findall(‘([1-9][0-9].[0-9]{1,2})?$‘,str)
7、带1-2位小数的正数或负数:^(\-)?\d+(\.\d{1,2})?$
import re str = ‘-21.33efsdfdf12.23‘ print re.search(‘^(\-)?\d+(\.\d{1,2})?‘,str).group()
参考链接
http://www.cnblogs.com/wupeiqi
本文出自 “crazyforever” 博客,请务必保留此出处http://crazyforever.blog.51cto.com/7362974/1736142
原文:http://crazyforever.blog.51cto.com/7362974/1736142