python正則表達式match和search用法實例
本文實例講述了python正則表達式match和search用法。分享給大家供大家參考。具體分析如下:
python提供了2中主要的正則表達式操作:re.match 和 re.search。
match :只從字符串的開始與正則表達式匹配,匹配成功返回matchobject,否則返回none;
search :將字符串的所有字串嘗試與正則表達式匹配,如果所有的字串都沒有匹配成功,返回none,否則返回matchobject;(re.search相當于perl中的默認行為)
import re
def testsearchandmatch():
s1="helloworld, i am 30 !"
w1 = "world"
m1 = re.search(w1, s1)
if m1:
print("find : %s" % m1.group())
if re.match(w1, s1) == none:
print("cannot match")
w2 = "helloworld"
m2 = re.match(w2, s1)
if m2:
print("match : %s" % m2.group())
testsearchandmatch()
#find : world
#cannot match
#match : helloworld
PS:關(guān)于正則,這里再為大家提供2款本站的正則表達式在線工具供大家參考使用(包正則生成、匹配與驗證等):
JavaScript正則表達式在線測試工具:http://tools.jb51.net/regex/javascript
正則表達式在線生成工具:http://tools.jb51.net/regex/create_reg
希望本文所述對大家的Python程序設(shè)計有所幫助。
- 淺談Python中re.match()和re.search()的使用及區(qū)別
- python用match()函數(shù)爬數(shù)據(jù)方法詳解
- Python中出現(xiàn)IndentationError:unindent does not match any outer indentation level錯誤的解決方法
- Python3中正則模塊re.compile、re.match及re.search函數(shù)用法詳解
- Python中正則表達式match()、search()函數(shù)及match()和search()的區(qū)別詳解
- Python3.9.1中使用match方法詳解
相關(guān)文章
python的sort函數(shù)與sorted函數(shù)排序問題小結(jié)
sort函數(shù)用于列表的排序,更改原序列而sorted用于可迭代對象的排序(包括列表),返回新的序列,這篇文章主要介紹了python的sort函數(shù)與sorted函數(shù)排序,需要的朋友可以參考下2023-07-07
python?matplotlib繪圖詳解大全(非常詳細!)
這篇文章主要給大家介紹了關(guān)于python?matplotlib繪圖詳解的相關(guān)資料,matplotlib是python中用于繪制各種圖像的模塊,功能十分強大,通常與pandas模塊搭配使用,可以生成各種樣視的圖片,用于數(shù)據(jù)的分析和展示,需要的朋友可以參考下2023-09-09
Python如何實現(xiàn)轉(zhuǎn)換URL詳解
這篇文章主要介紹了Python如何實現(xiàn)轉(zhuǎn)換URL詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-07-07

