python使用Regex

來源:互聯網
上載者:User

python中使用Regex

1. 匹配字元

Regex中的元字元有 . ^ $ * + ? { } [ ] \ | ( )

匹配字元用的模式有

\d 匹配任一數字

\D 匹配任意非數字

\s 匹配任意空白字元

\S 匹配任意非空格字元

\w 匹配任一數字或者字母

\W 匹配任意非數字或者字母

2. Regex

python中使用compile處理Regex,如:

import re;

p=re.compile(‘[a-c]’);

p.match(s);

s是待匹配的字串,match是匹配方法,類似的方法有

match() 確定從行首開始匹配

search() 在任意位置匹配

findall() 找匹配的所有子串,並作為子串返回

finditer() 找匹配的所有子串,並且以迭代器的形式返回

match也有很多方法,如:

group() 把Regex匹配的字串返回

start() 返回匹配的起點

end() 返回匹配的終點

span() 返回匹配的(start,end)的元組

例1:>>>import re;

>>>p=re.compile(‘^[a-c]’)

>>>q=p.match(“abcd”);

>>>print q.group()

ab

>>>>q.span()

(0,2)

例2:

>>>import re

>>>p=re.compile(‘\d+’);

>>>q=p.findall(‘1 and 10 and 20’);

>>>print q

[‘1’,’2’,’3’]

上述的匹配也可以使用另外一種形式,即:

re.match(’\d+’,’d23r’)

例3:

>>>p=re.match(’\d+’,’d23r’)

>>>print p

None

匹配的其它參數:

re.compile(‘[a-c]’,re.I) re.I 表示忽略大小寫

re.compile(‘^ab$’,re.M) re.M 表示^或者$在行首和行尾以及字串的開頭和結束都會進行匹配。如果不加這個標誌,只會在字串的開始和結束之處匹配

例4:

re.compile(“””

[1-3] #1-3

[a-c] # a-c

“””,re.VERBOSE

) re.VERBOSE 的出現使得Regex可以再多行出現,而且可以在每行加上注釋

上面的匹配相當於 re.compile(‘[1-3][a-c]’)

3. 分組

使用()來進行分組

例5:

>>>p=re.compile(‘(12)+’)

>>>m=p.match(‘121212’)

>>>print m.group()

121212

上面匹配的是12重複出現一次或多次

還可以列印分組的資訊,

>>>print m.group(1)

12

python會自動捕獲分組的資訊,如果不想捕獲分組的資訊,可以使用?:

例6:

>>>import re

>>>s =”hello ab1cd”;

>>>p=re.search(‘(?:h.*)(a.*)(c.*)’);

>>>print “a* {0}”.format(p.group(1))

a* ab

>>>print “c* {0}”.format(p.group(2))

c* cd

p.group(0)存放的是整個運算式的匹配情況,p.group(1)存放的是(a.*)的匹配資訊,p.group(2)存放的是(b.*)的匹配資訊,而 h.*前面因為有?:沒有被捕獲

如果分組過多,使用組的標號還是顯得麻煩,這時可以對組進行命名,然後可以通過名字來使用它們。

例7:

>>>import re;

>>>s=”hello ab1cd”

>>>p=re.search(‘(?P<a>a.*)(?P<c>c.*)’);

>>>print “a* {0}”.format(p.group(‘a’)

a* ab

>>>print “c* {0}”.format(p.group(‘c’))

c* cd

4.貪婪和非貪婪模式

在貪婪模式下,* + 都會儘可能地多匹配字元,如:

例8:

>>>import re;

>>>p=re.compile('<h.*></h>');

>>>m=p.findall('<h1></h><h2></h>')

>>>print m;

[‘<h1></h><h2></h>’]

有時希望它匹配出<h1></h>,<h2></h>兩個結果,這時就可以用非貪婪模式了。只要在*或.後面加上?。

例9:

>>>import re;

>>>p=re.compile('<h.*?></h>');

>>>m=p.findall('<h1></h><h2></h>')

>>>print m;

[‘<h1></h>’,’<h2></h>’]

5. 前向定界符和後向定界符

如果先匹配模式A,在匹配模式B,可以使用A(?=B),如果先匹配模式A,而且希望後面沒有B,可以使用A(?!B).

例10:

>>>import re;

>>>s=”ab2cd”

>>>m=re.search(“ab2(?=cd)”,s);

>>>print m.group();

ab2cd

例11:

>>>import re;

>>>s='ab2cd'
>>>m=re.search('ab2(?!cd)',s);
>>>print m

None

類似地,如果匹配模式B,同時在其前面要有模式A,可以使用(?<=A)B的形式,

如果匹配模式B,同時在其前面沒有模式A,可以使用(?<!A)B的形式

例12:

>>>import re;

>>>s=”ab2cd”;

>>>m=re.search(‘(?<=ab2)cd’,s)

>>>print m.group()

cd

例13:

>>>import re

>>>string= "ab2cd"
>>>pattern = re.search(r'(?<!ab2)cd',string)
>>>print pattern;

None

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.