正則表達式是用於處理字符串的強大工具,擁有自己獨特的語法和自己獨立的處理機制,在提供了有正則表達式的語言裏,正則表達式的語法是一樣的,只是不同的語言所支持的語法數量不同。
正則表達式實現的流程爲:
下面具體介紹正則表達式
在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