Python模塊學習 re 正則表達式
更新時間:2011年05月19日 01:06:07 作者:
今天學習了Python中有關正則表達式的知識。關于正則表達式的語法,不作過多解釋,網(wǎng)上有許多學習的資料。這里主要介紹Python中常用的正則表達式處理函數(shù)
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的函數(shù)原型為:re.match(pattern, string, flags)
第一個參數(shù)是正則表達式,這里為"(\w+)\s",如果匹配成功,則返回一個Match,否則返回一個None;
第二個參數(shù)表示要匹配的字符串;
第三個參數(shù)是標致位,用于控制正則表達式的匹配方式,如:是否區(qū)分大小寫,多行匹配等等。
re.search
re.search函數(shù)會在字符串內(nèi)查找模式匹配,只到找到第一個匹配然后返回,如果字符串沒有匹配,則返回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的函數(shù)原型為: re.search(pattern, string, flags)
每個參數(shù)的含意與re.match一樣。
re.match與re.search的區(qū)別:re.match只匹配字符串的開始,如果字符串開始不符合正則表達式,則匹配失敗,函數(shù)返回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的函數(shù)原型為:re.sub(pattern, repl, string, count)
其中第二個函數(shù)是替換后的字符串;本例中為'-'
第四個參數(shù)指替換個數(shù)。默認為0,表示每個匹配項都替換。
re.sub還允許使用函數(shù)對匹配項的替換進行復雜的處理。如: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
可以把正則表達式編譯成一個正則表達式對象。可以把那些經(jīng)常使用的正則表達式編譯成正則表達式對象,這樣可以提高一定的效率。下面是一個正則表達式對象的一個例子:
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'的單詞用[]括起來。
更詳細的內(nèi)容,可以參考Python手冊。
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的函數(shù)原型為:re.match(pattern, string, flags)
第一個參數(shù)是正則表達式,這里為"(\w+)\s",如果匹配成功,則返回一個Match,否則返回一個None;
第二個參數(shù)表示要匹配的字符串;
第三個參數(shù)是標致位,用于控制正則表達式的匹配方式,如:是否區(qū)分大小寫,多行匹配等等。
re.search
re.search函數(shù)會在字符串內(nèi)查找模式匹配,只到找到第一個匹配然后返回,如果字符串沒有匹配,則返回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的函數(shù)原型為: re.search(pattern, string, flags)
每個參數(shù)的含意與re.match一樣。
re.match與re.search的區(qū)別:re.match只匹配字符串的開始,如果字符串開始不符合正則表達式,則匹配失敗,函數(shù)返回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的函數(shù)原型為:re.sub(pattern, repl, string, count)
其中第二個函數(shù)是替換后的字符串;本例中為'-'
第四個參數(shù)指替換個數(shù)。默認為0,表示每個匹配項都替換。
re.sub還允許使用函數(shù)對匹配項的替換進行復雜的處理。如: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
可以把正則表達式編譯成一個正則表達式對象。可以把那些經(jīng)常使用的正則表達式編譯成正則表達式對象,這樣可以提高一定的效率。下面是一個正則表達式對象的一個例子:
復制代碼 代碼如下:
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'的單詞用[]括起來。
更詳細的內(nèi)容,可以參考Python手冊。
您可能感興趣的文章:
- python正則表達式(re模塊)的使用詳解
- python的正則表達式和re模塊詳解
- python re模塊和正則表達式
- 正則表達式+Python re模塊詳解
- 詳解Python正則表達式re模塊
- 淺談python中的正則表達式(re模塊)
- python模塊之re正則表達式詳解
- Python的re模塊正則表達式操作
- Python基礎教程之正則表達式基本語法以及re模塊
- python re正則表達式模塊(Regular Expression)
- python正則表達式re模塊詳解
- python正則表達式re模塊詳細介紹
- python的正則表達式re模塊的常用方法
- PYTHON正則表達式 re模塊使用說明
- Python中的re正則表達式模塊
相關文章
python連接mongodb操作數(shù)據(jù)示例(mongodb數(shù)據(jù)庫配置類)
這篇文章主要介紹了python連接mongodb操作數(shù)據(jù)示例,主要包括插入數(shù)據(jù)、更新數(shù)據(jù)、查詢數(shù)據(jù)、刪除數(shù)據(jù)等2013-12-12使用Python開發(fā)windows GUI程序入門實例
這篇文章主要介紹了使用Python開發(fā)windows GUI程序入門實例,本文著重介紹開發(fā)環(huán)境必須的軟件,代碼實現(xiàn)相對簡單,需要的朋友可以參考下2014-10-10Django多數(shù)據(jù)庫的實現(xiàn)過程詳解
這篇文章主要介紹了Django多數(shù)據(jù)庫的實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08Python監(jiān)聽剪切板實現(xiàn)方法代碼實例
這篇文章主要介紹了Python監(jiān)聽剪切板實現(xiàn)方法代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11