Python 字串匹配 match

2021-09-19 13:42:43 字數 2043 閱讀 4584

import reprint(re.match("abc","abc")) #匹配,左邊第乙個開始算起來,print(re.match("xabc","abc"))#匹配不成功返回none,匹配成功返回位置詳細資訊print(re.match("abc","xabc"))print(re.match("abc","abcx"))

import re#match嚴格匹配,從第乙個開始,"abc"在"abcdefgabc"出現一次matchobj=re.match("abc","abcdefgabc")print(matchobj) #print(type(matchobj))print(matchobj.group(0)) #挖掘的第乙個匹配

import re#(.*)  .任意字元不包含換行,*0-n次line="zhangsan is a boy not a gril"matchobj=re.match(r"(.*) is (.*) not (.*)",line)print(matchobj)    #詳細的匹配print(matchobj.group(0))  #zhangsan is a boy not a grilprint(matchobj.group(1))  #zhangsanprint(matchobj.group(2))  #a boyprint(matchobj.group(3))  #a gril

#3個正規表示式必須匹配group(1) group(2)group(3)

import re#切割,相當於字串的split()line="827007914----penghueix"matchobj=re.match(r"(.*)----(.*)",line)print(matchobj)print(matchobj.group(0))  #827007914----penghueixprint(matchobj.group(1))  #827007914print(matchobj.group(2))  #penghueix

import reprint(re.match("abc","abc")) #匹配,左邊第乙個開始算起來,print(re.match("xabc","abc"))#匹配不成功返回none,匹配成功返回位置詳細資訊print(re.match("abc","xabc"))print(re.match("abc","abcx"))

import re#match嚴格匹配,從第乙個開始,"abc"在"abcdefgabc"出現一次matchobj=re.match("abc","abcdefgabc")print(matchobj) #print(type(matchobj))print(matchobj.group(0)) #挖掘的第乙個匹配

import re#(.*)  .任意字元不包含換行,*0-n次line="zhangsan is a boy not a gril"matchobj=re.match(r"(.*) is (.*) not (.*)",line)print(matchobj)    #詳細的匹配print(matchobj.group(0))  #zhangsan is a boy not a grilprint(matchobj.group(1))  #zhangsanprint(matchobj.group(2))  #a boyprint(matchobj.group(3))  #a gril

#3個正規表示式必須匹配group(1) group(2)group(3)

import re#切割,相當於字串的split()line="827007914----penghueix"matchobj=re.match(r"(.*)----(.*)",line)print(matchobj)print(matchobj.group(0))  #827007914----penghueixprint(matchobj.group(1))  #827007914print(matchobj.group(2))  #penghueix

python 字串匹配問題

想匹配html 安裝最新版python 各種資料庫的注釋 中的 56845037 和 59120585 嘗試用正則 pattern l r re.findall pattern l,html 結果不成功。返回為空,有用 soup beautifulsoup html,lxml print soup....

字串匹配

題目描述 讀入資料string 然後讀入乙個短字串。要求查詢string 中和短字串的所有匹配,輸出行號 匹配字串。匹配時不區分大小寫,並且可以有乙個用中括號表示的模式匹配。如 aa 123 bb 就是說aa1bb aa2bb aa3bb都算匹配。輸入 輸入有多組資料。每組資料第一行輸入n 1 n ...

字串匹配

time limit 1000ms memory limit 65536k 給定兩個字串string1和string2,判斷string2是否為string1的子串。輸入包含多組資料,每組測試資料報含兩行,第一行代表string1,第二行代表string2,string1和string2中保證不出現...