python正則表示式匹配指定的字元開頭和指定的字元結束
阿新 • • 發佈:2021-08-31
一,使用python的re.findall函式,匹配指定的字元開頭和指定的字元結束
程式碼示例:
1 import re 2 # re.findall函式;匹配指定的字串開頭和指定的字串結尾(前後不包含指定的字串) 3 str01 = 'hello word' 4 str02 = re.findall('(?<=e).*?(?=r)',str01) 5 print(str02)
輸出結果:
1 ['llo wo']
二,使用python的re.findall函式,匹配指定的字元開頭和指定的字元結束(前後包含指定的字串)
注意:
- 在 re.findall()的第一個引數中輸入的為 'h.*o' 可以匹配到相同的值直到最後一個值;
程式碼示例:
1 import re 2 # re.findall函式;匹配指定的字串開頭和指定的字串結尾(前後包含指定的字串) 3 str01 = 'hello word' 4 str02 = re.findall('h.*o',str01) 5 print(str02)
輸出結果:
1 ['hello wo']
- 如果引數為 'h.*?o',則只匹配到第一個值
1 import re 2 # re.findall函式; .*? 如果匹配的字元中有多個相同的匹配結尾值的 3 str01 = 'hello word' 4 str02 = re.findall('h.*?o',str01) 5print(str02)
輸出結果:
1 ['hello']
import re
# re.findall函式;匹配指定的字串開頭和指定的字串結尾(前後包含指定的字串)
str01 = 'hello word'
str02 = re.findall('h.*o',str01)
print(str02)