日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

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

作者:胡小牧 ? 更新時間: 2023-05-03 編程語言

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 注意點

注意當(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 為了增加可讀性,忽略空格和 # 后面的注釋

原文鏈接:https://blog.csdn.net/qq_33210042/article/details/116794784

欄目分類
最近更新