欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)字符串模糊匹配的兩種實(shí)現(xiàn)方法

 更新時(shí)間:2023年11月23日 09:47:29   作者:禺垣  
本文主要介紹了Python實(shí)現(xiàn)字符串模糊匹配的兩種實(shí)現(xiàn)方法,Python中通過(guò)re.search()方法實(shí)現(xiàn),對(duì)于首位起始的內(nèi)容匹配,也可通過(guò)re.match()方法實(shí)現(xiàn),感興趣的可以了解一下

在一個(gè)字符串中,有時(shí)需對(duì)其中某些內(nèi)容進(jìn)行模糊匹配以實(shí)現(xiàn)條件的判定,如在“你好,hello,world”中判斷是否含有“llo”。Python中通過(guò)re.search()方法實(shí)現(xiàn),特別地,對(duì)于首位起始的內(nèi)容匹配,也可通過(guò)re.match()方法實(shí)現(xiàn)。若匹配成功,它們返回一個(gè)re.Match對(duì)象;若匹配失敗,返回None。

re.search()實(shí)現(xiàn)模糊匹配

import re

teststr = "你好,hello,world"
print('\n',teststr,'\n')

pattern1 = "llo"
r1 = re.search(pattern1, teststr)
if r1:
    print(pattern1,'匹配成功.')
else:
    print(pattern1,'匹配失敗.')


pattern2 = "你好"
r2 = re.search(pattern2, teststr)
if r2:
    print(pattern2,"匹配成功.")
else:
    print(pattern2,"匹配失敗.")

re.match()實(shí)現(xiàn)首位起始的模糊匹配

teststr = "你好,hello,world"
print('\n',teststr,'\n')

pattern1 = "llo"
r1 = re.match(pattern1, teststr)
if r1:
    print(pattern1,'匹配成功.')
else:
    print(pattern1,'匹配失敗.')

pattern2 = "你好"
r2 = re.match(pattern2, teststr)
if r2:
    print(pattern2,"匹配成功.")
else:
    print(pattern2,"匹配失敗.")

到此這篇關(guān)于Python實(shí)現(xiàn)字符串模糊匹配的兩種實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Python 字符串模糊匹配內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python隨機(jī)生成一個(gè)6位的驗(yàn)證碼代碼分享

    Python隨機(jī)生成一個(gè)6位的驗(yàn)證碼代碼分享

    這篇文章主要介紹了Python隨機(jī)生成一個(gè)6位的驗(yàn)證碼代碼分享,本文直接給出代碼實(shí)例,需要的朋友可以參考下
    2015-03-03
  • 最新評(píng)論