python正則表達式函數(shù)match()和search()的區(qū)別
match()函數(shù)只檢測RE是不是在string的開始位置匹配, search()會掃描整個string查找匹配, 也就是說match()只有在0位置匹配成功的話才有返回,如果不是開始位置匹配成功的話,match()就返回none
例如:
#! /usr/bin/env python
# -*- coding=utf-8 -*-
import re
text= 'pythontab'
m= re.match(r"\w+", text)
if m:
print m.group(0)
else:
print 'not match'
結果是:pythontab
而:
#! /usr/bin/env python
# -*- coding=utf-8 -*-
#
import re
text= '@pythontab'
m= re.match(r"\w+", text)
if m:
print m.group(0)
else:
print 'not match'
結果是:not match
search()會掃描整個字符串并返回第一個成功的匹配
例如:
#! /usr/bin/env python
# -*- coding=utf-8 -*-
#
import re
text= 'pythontab'
m= re.search(r"\w+", text)
if m:
print m.group(0)
else:
print 'not match'
結果是:pythontab
那這樣呢:
#! /usr/bin/env python
# -*- coding=utf-8 -*-
#
import re
text= '@pythontab'
m= re.search(r"\w+", text)
if m:
print m.group(0)
else:
print 'not match'
結果是:pythontab
更多關于python正則函數(shù)請查看下面的相關文章
相關文章
torchtext入門教程必看,帶你輕松玩轉文本數(shù)據(jù)處理
這篇文章主要介紹了torchtext入門教程必看,帶你輕松玩轉文本數(shù)據(jù)處理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
python 實現(xiàn)format進制轉換與刪除進制前綴
這篇文章主要介紹了python 實現(xiàn)format進制轉換與刪除進制前綴的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Python的Flask框架中使用Flask-SQLAlchemy管理數(shù)據(jù)庫的教程
在Python中我們可以使用SQLAlchemy框架進行數(shù)據(jù)庫操作,那么對應的在Flask框架中我們可以使用SQLAlchemy,下面我們就來看一下Python的Flask框架中使用Flask-SQLAlchemy管理數(shù)據(jù)庫的教程2016-06-06
Python使用requests庫發(fā)送請求的示例代碼
與原生的urllib庫相比,requests庫提供了更簡潔、易于理解和使用的API,使發(fā)送HTTP請求變得更加直觀和高效,所以本文給大家介紹了Python如何使用requests庫發(fā)送請求,需要的朋友可以參考下2024-03-03

