最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python re.match函數(shù)的具體使用

 更新時間:2023年02月13日 10:36:05   作者:胡小牧  
本文主要介紹了python re.match函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1 re.match 說明

re.match()  從開始位置開始往后查找,返回第一個符合規(guī)則的對象,如果開始位置不符合匹配隊形則返回None

從源碼里面看下match 里面的內(nèi)容

里面有3個參數(shù) pattern ,string ,flags 

pattern : 是匹配的規(guī)則內(nèi)容

string : 要匹配的字符串

flag : 標(biāo)志位(這個是可選的,可寫,可不寫),用于控制正則表達(dá)式的匹配方式,如:是否區(qū)分大小寫,多行匹配等等

下面寫一個demo

str_content = "Python is a good language"  # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python"  # pattern 匹配的規(guī)則
re_content = re.match("Python", str_content)
print(re_content)

打印的結(jié)果如下

可以看到匹配的的下標(biāo)是(0,6) 匹配的內(nèi)容是Python

2 span 的使用

如果想獲取匹配的下標(biāo),可以使用span ,

match span 的作用就是返回匹配到內(nèi)容的下標(biāo)

使用方式如下

import re  # 導(dǎo)入re 模塊
 
str_content = "Python is a good language"  # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python"  # pattern 匹配的規(guī)則
re_content = re.match("Python", str_content).span()
print(re_content)

打印結(jié)果如下

3 group 的使用

如果想獲取匹配到結(jié)果的內(nèi)容可以使用group ,注意使用group的時候就不要在使用span 了

import re  # 導(dǎo)入re 模塊
 
str_content = "Python is a good language"  # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python"  # pattern 匹配的規(guī)則
re_content = re.match("Python", str_content)
print(re_content.group())

打印結(jié)果如下

4 匹配不到內(nèi)容的情況

如下面的返回結(jié)果為None

import re  # 導(dǎo)入re 模塊
 
str_content = "Python is a good language"  # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python"  # pattern 匹配的規(guī)則
re_content = re.match("python", str_content)
print(re_content)
# 或者
 
str_content = "Python is a good language"  # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python"  # pattern 匹配的規(guī)則
re_content = re.match("is", str_content)
print(re_content)

5 使用group 注意點(diǎn)

注意當(dāng)匹配不到內(nèi)容的時候就使用group 或者span 的時候會報錯,所以當(dāng)使用group 的時候 先判斷下是否匹配到內(nèi)容然后在使用它

例如匹配不到內(nèi)容的情況下使用group

import re  # 導(dǎo)入re 模塊
 
str_content = "Python is a good language"  # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python"  # patterPn 匹配的規(guī)則
re_content = re.match("python", str_content)
print(re_content.group())

這樣會報錯,報錯內(nèi)容如下

添加是否匹配判斷

import re  # 導(dǎo)入re 模塊
 
str_content = "Python is a good language"  # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python"  # patterPn 匹配的規(guī)則
re_content = re.match("python", str_content)
if re_content:
    print(re_content.group())
else:
    print("沒有匹配到內(nèi)容")

打印結(jié)果如下

這樣會走到else 里面就不會報錯了

6 flag 的使用

寫一個忽略大小寫的情況

import re  # 導(dǎo)入re 模塊
 
str_content = "Python is a good language"  # 要匹配的內(nèi)容, 對應(yīng)match 里面的string
str_pattern = "Python"  # patterPn 匹配的規(guī)則
re_content = re.match("python", str_content, re.I)
if re_content:
    print(re_content.group())
else:
    print("沒有匹配到內(nèi)容")

打印結(jié)果如下:

flags : 可選,表示匹配模式,比如忽略大小寫,多行模式等,具體參數(shù)為:

  • re.I 忽略大小寫
  • re.L 表示特殊字符集 \w, \W, \b, \B, \s, \S 依賴于當(dāng)前環(huán)境
  • re.M 多行模式
  • re.S 即為 . 并且包括換行符在內(nèi)的任意字符(. 不包括換行符)
  • re.U 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依賴于 Unicode 字符屬性數(shù)據(jù)庫
  • re.X 為了增加可讀性,忽略空格和 # 后面的注釋

到此這篇關(guān)于python re.match函數(shù)的具體使用的文章就介紹到這了,更多相關(guān)python re.match內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

霍州市| 夏邑县| 建昌县| 莱芜市| 久治县| 和龙市| 龙胜| 徐州市| 噶尔县| 湄潭县| 全南县| 蒙阴县| 新昌县| 东乡县| 濮阳县| 九江县| 汉寿县| 肇源县| 都江堰市| 禹城市| 金阳县| 太仆寺旗| 鄢陵县| 黄梅县| 石狮市| 永德县| 嘉善县| 辽阳市| 建湖县| 乡宁县| 江安县| 六枝特区| 乐安县| 云林县| 隆子县| 涿州市| 山丹县| 上杭县| 长垣县| 珲春市| 岑巩县|