Python中re.match與re.search的使用方法詳解

來源:互聯網
上載者:User

標籤:

本文和大家分享的主要是pythonRegex中re.match函數與re.search方法的相關用法及異同點,希望通過本文的分享,能對大家有所協助。

re.match函數

re.match 嘗試從字串的起始位置匹配一個模式,如果不是起始位置匹配成功的話,match()就返回none。

函數文法:

re.match(pattern, string, flags=0)

函數參數說明:

參數描述

pattern匹配的Regex

string要匹配的字串。

flags標誌位,用於控制Regex的匹配方式,如:是否區分大小寫,多行匹配等等。

匹配成功re.match方法返回一個匹配的對象,否則返回None。

我們可以使用group(num) 或 groups() 匹配對象函數來擷取匹配運算式。

匹配對象方法描述

group(num=0)匹配的整個運算式的字串,group() 可以一次輸入多個組號,在這種情況下它將返回一個包含那些組所對應值的元組。

groups()返回一個包含所有小組字串的元組,從 1 到 所含的小組號。

執行個體 1:

#!/usr/bin/python# -*- coding: UTF-8 -*-

import reprint(re.match(’www’, ’www.runoob.com’).span()) # 在起始位置匹配print(re.match(’com’, ’www.runoob.com’)) # 不在起始位置匹配

以上執行個體運行輸出結果為:

(0, 3)None

執行個體 2:

#!/usr/bin/pythonimport re

line = "Cats are smarter than dogs"

matchObj = re.match( r’(.*) are (.*?) .*’, line, re.M|re.I)

if matchObj:

print "matchObj.group() : ", matchObj.group()

print "matchObj.group(1) : ", matchObj.group(1)

print "matchObj.group(2) : ", matchObj.group(2)else:

print "No match!!"

以上執行個體執行結果如下:

matchObj.group() : Cats are smarter than dogs

matchObj.group(1) : Cats

matchObj.group(2) : smarter

 

re.search方法

re.search 掃描整個字串並返回第一個成功的匹配。

函數文法:

re.search(pattern, string, flags=0)

函數參數說明:

參數描述

pattern匹配的Regex

string要匹配的字串。

flags標誌位,用於控制Regex的匹配方式,如:是否區分大小寫,多行匹配等等。

匹配成功re.search方法返回一個匹配的對象,否則返回None。

我們可以使用group(num) 或 groups() 匹配對象函數來擷取匹配運算式。

匹配對象方法描述

group(num=0)匹配的整個運算式的字串,group() 可以一次輸入多個組號,在這種情況下它將返回一個包含那些組所對應值的元組。

groups()返回一個包含所有小組字串的元組,從 1 到 所含的小組號。

執行個體 1:

#!/usr/bin/python# -*- coding: UTF-8 -*-

import reprint(re.search(’www’, ’www.runoob.com’).span()) # 在起始位置匹配print(re.search(’com’, ’www.runoob.com’).span()) # 不在起始位置匹配

以上執行個體運行輸出結果為:

(0, 3)(11, 14)

執行個體 2:

#!/usr/bin/pythonimport re

line = "Cats are smarter than dogs";

searchObj = re.search( r’(.*) are (.*?) .*’, line, re.M|re.I)

if searchObj:

print "searchObj.group() : ", searchObj.group()

print "searchObj.group(1) : ", searchObj.group(1)

print "searchObj.group(2) : ", searchObj.group(2)else:

print "Nothing found!!"

以上執行個體執行結果如下:

searchObj.group() : Cats are smarter than dogs

searchObj.group(1) : Cats

searchObj.group(2) : smarter

 

re.match與re.search的區別

re.match只匹配字串的開始,如果字串開始不符合Regex,則匹配失敗,函數返回None;而re.search匹配整個字串,直到找到一個匹配。

執行個體:

#!/usr/bin/pythonimport re

line = "Cats are smarter than dogs";

matchObj = re.match( r’dogs’, line, re.M|re.I)if matchObj:

print "match --> matchObj.group() : ", matchObj.group()else:

print "No match!!"

matchObj = re.search( r’dogs’, line, re.M|re.I)if matchObj:

print "search --> matchObj.group() : ", matchObj.group()else:

print "No match!!"

以上執行個體運行結果如下:

No match!!

search --> matchObj.group() : dogs

 

Python中re.match與re.search的使用方法詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.