想要在python中使用正則表達式,就需要先導入re模塊,正則表達式是一個強大的功能,可以為我們節省很多工作量。
一、元字符:
用一些具有特殊含義的符號表示特定種類的字符或位置。
. 匹配除換行符以外的任意字符
\w匹配字母或數字或下划線或漢字
\W匹配任何非字母數字或下划線或漢字
\s匹配任意的空白符
\d匹配數字
\D匹配非數字字符
\b匹配單子的開始或結束
^匹配字符串的開始,如果放在字符串的開頭,則表示取非。
$匹配字符串的結束
匹配次數
*重復零次或多次
+重復一次或更多次
?重復零次或一次
{n}重復n次
{n,}重復n次或多次
{n,m}重復n到m次。
范圍
[]用來匹配一個指定的字符類別,所謂的字符類別就是你想匹配的一個字符集,對於字符集中的字符可以理解成或的關系。
[0-9] 匹配0~9的數字,同\d
[a-z]匹配所有的小寫字母
[A-Z]匹配所有的大寫字母
[a-zA-Z] 匹配所有的字母
[a-z0-9A-Z] 等同於\w
字符串轉義
如果想匹配元字符本身或者正則中的一些特殊字符,使用\
轉義。例如匹配*
這個字符則使用\*
,匹配\
這個字符,使用\\
。
需要轉義的字符:$
, (
, )
, *
, +
, .
, [
, ]
, ?
, \
, ^
, {
, }
, |
為了避免過多\的使用,python提供了原生字符的方法,也就是在字符串前面加上一個“r”,代表此字符串中的“\”可直接用於正則表達式,而不用再次轉義。因此,請養成在python的正則表達式字符串的前面添加一個“r“的好習慣。
二、re模塊的方法
1、match
re.match(' 規則','字符串 ') 從字符串的開頭進行匹配,匹配單個。
2、search
re.search(' ',' ') 在字符串中進行匹配,並返回第一個匹配到的值。
3、findall
re.findall('','') 在字符串中進行匹配,並以列表的形式
返回所有滿足的值。
>>> re.findall('\d+','dsg2335dhreh54623grh46fdh57')
['2335', '54623', '46', '57']
4、group,groups
a = "123abc456" print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group() print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(0) print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(1) print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(2) print re.search("([0-9]*)([a-z]*)([0-9]*)", a).groups()
5、sub
sub(pattern, repl, string, count=0, flags=0)用於替換匹配到的字符串。
>>> import re >>> a = 'sfgwg323dgw13' >>> b = re.sub(r'\d+','111',a) >>> b 'sfgwg111dgw111'
6、split(pattern, string, maxsplit=0, flags=0) 根據指定匹配進行分組
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
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
inpp = '1-2*((60-30 +(-40-5)*(9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2))' inpp = re.sub('\s*','',inpp) new_content = re.split('\(([\+\-\*\/]?\d+[\+\-\*\/]?\d+){1}\)', inpp, 1) print new_content