Python re正規表示式學習

2021-06-06 04:44:07 字數 4425 閱讀 6939

2011-04-28 23:16:12

標籤:python re 學習

休閒職場

一、re.match

re.match 嘗試從字串的開始匹配乙個模式,如:下面的例子匹配第乙個單詞。

import re  

text = "jgood is a handsome boy, he is cool, clever, and so on..."

m = re.match(r"(\w+)\s", text)  

if m:  

print m.group(0), '\n', m.group(1)  

else:  

print 'not match'

re.match的函式原型為:re.match(pattern, string, flags)

第乙個引數是正規表示式,這裡為"(\w+)\s",如果匹配成功,則返回乙個match,否則返回乙個none;

第二個引數表示要匹配的字串;

第三個引數是標緻位,用於控制正規表示式的匹配方式,如:是否區分大小寫,多行匹配等等。

二、re.search

re.search函式會在字串內查詢模式匹配,只到找到第乙個匹配然後返回,如果字串沒有匹配,則返回none。

import re  

text = "jgood is a handsome boy, he is cool, clever, and so on..."

m = re.search(r'\shan(ds)ome\s', text)  

if m:  

print m.group(0), m.group(1)  

else:  

print 'not search'

re.search的函式原型為: re.search(pattern, string, flags)

每個引數的含意與re.match一樣。

re.match與re.search的區別:re.match只匹配字串的開始,如果字串開始不符合正規表示式,則匹配失敗,函式返回none;而re.search匹配整個字串,直到找到乙個匹配。

三、re.sub

re.sub用於替換字串中的匹配項。下面乙個例子將字串中的空格 ' ' 替換成 '-' :

import re  

text = "jgood is a handsome boy, he is cool, clever, and so on..."

print re.sub(r'\s+', '-', text)

re.sub的函式原型為:re.sub(pattern, repl, string, count)

其中第二個函式是替換後的字串;本例中為'-'

第四個引數指替換個數。預設為0,表示每個匹配項都替換。

re.sub還允許使用函式對匹配項的替換進行複雜的處理。如:re.sub(r'\s', lambda m: '[' + m.group(0) + ']', text, 0);將字串中的空格' '替換為'[ ]'。

四、re.split

可以使用re.split來分割字串,如:re.split(r'\s+', text);將字串按空格分割成乙個單詞列表。

五、re.findall

re.findall可以獲取字串中所有匹配的字串。如:re.findall(r'\w*oo\w*', text);獲取字串中,包含'oo'的所有單詞。

六、re.compile

可以把正規表示式編譯成乙個正規表示式物件。可以把那些經常使用的正規表示式編譯成正規表示式物件,這樣可以提高一定的效率。下面是乙個正規表示式物件的乙個例子:

import re  

text = "jgood is a handsome boy, he is cool, clever, and so on..."

regex = re.compile(r'\w*oo\w*')  

print regex.findall(text)   #查詢所有包含'oo'的單詞  

print regex.sub(lambda m: '[' + m.group(0) + ']', text) #將字串中含有'oo'的單詞用括起來。

七、group()

1.group([group1,…])

返回匹配到的乙個或者多個子組。如果是乙個引數,那麼結果就是乙個字串,如果是多個引數,那麼結果就是乙個引數乙個item的元組。group1的 預設值為0(將返回所有的匹配值).如果groupn引數為0,相對應的返回值就是全部匹配的字串,如果group1的值是[1…99]範圍之內的,那 麼將匹配對應括號組的字串。如果組號是負的或者比pattern中定義的組號大,那麼將丟擲indexerror異常。如果pattern沒有匹配到, 但是group匹配到了,那麼group的值也為none。如果乙個pattern可以匹配多個,那麼組對應的是樣式匹配的最後乙個。另外,子組是根據括 號從左向右來進行區分的。

>>> m=re.match("(\w+) (\w+)","abcd efgh, chaj")

>>> m.group()            # 匹配全部

'abcd efgh'

>>> m.group(1)     # 第乙個括號的子組.

'abcd'

>>> m.group(2)

'efgh'

>>> m.group(1,2)           # 多個引數返回乙個元組

('abcd', 'efgh')

>>> m=re.match("(?p\w+) (?p\w+)","sam lee")

>>> m.group("first_name")  #使用group獲取含有name的子組

'sam'

>>> m.group("last_name")

'lee'

下面把括號去掉

>>> m=re.match("\w+ \w+","abcd efgh, chaj")

>>> m.group()

'abcd efgh'

>>> m.group(1)

traceback (most recent call last):

file "", line 1, in

m.group(1)

indexerror: no such group

if a group matches multiple times, only the last match is accessible:

如果乙個組匹配多個,那麼僅僅返回匹配的最後乙個的。

>>> m=re.match(r"(..)+","a1b2c3")

>>> m.group(1)

'c3'

>>> m.group()

'a1b2c3'

group的預設值為0,返回正規表示式pattern匹配到的字串

>>> s="afkak1aafal12345adadsfa"

>>> pattern=r"(\d)\w+(\d)\w"

>>> m=re.match(pattern,s)

>>> print m

none

>>> m=re.search(pattern,s)

>>> m

<_sre.sre_match object at>

>>> m.group()

'1aafal12345a'

>>> m.group(1)

'1'>>> m.group(2)

'45'

>>> m.group(1,2,0)

('1', '45', '1aafal12345a')

2.groups([default])

返回乙個包含所有子組的元組。default是用來設定沒有匹配到組的預設值的。default預設是"none」,

>>> m=re.match("(\d+)\.(\d+)","23.123")

>>> m.groups()

('23', '123')

>>> m=re.match("(\d+)\.?(\d+)?","24") #這裡的第二個\d沒有匹配到,使用預設值"none"

>>> m.groups()

('24', none)

>>> m.groups("0")

('24', '0')

3.groupdict

([default])

返回匹配到的所有命名子組的字典。key是name值,value是匹配到的值。引數default是沒有匹配到的子組的預設值。這裡與groups()方法的引數是一樣的。預設值為none

>>> m=re.match("(\w+) (\w+)","hello world")

>>> m.groupdict()

{}>>> m=re.match("(?p\w+) (?p\w+)","hello world")

>>> m.groupdict()

通過上例可以看出,groupdict()對沒有name的子組不起作用

Python re 正規表示式

import re 匯入re模 result re.match 正規表示式,要匹配的字串 使用match 方法進行匹配操作 result.group 使用group 方法提取資料 案例 匹配變數名是否符合識別符號命名規則 res re.match a za z w name 123 print re...

正規表示式 python re

字元功能 或 分組 num 引用分組num匹配到的字串 p 分組起別名 p name 引用別名為name分組匹配到的字串 示例import re label res re.match r w w label print res.group www.itcast.cn h1 html import r...

python re 正規表示式

1 re.match str id s paragraph.text re.match 表示從變數 paragraph.text 所代表的 字串 的開始匹配模式 str id s 模式 str id s 表示 以id 某個數字 開始,s 表示0個或者多個空白符。表示結尾。2 searchobj re...