一,使用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) 5 print(str02)
輸出結果:
1 ['hello']
import re
# re.findall函數;匹配指定的字符串開頭和指定的字符串結尾(前后包含指定的字符串)
str01 = 'hello word'
str02 = re.findall('h.*o',str01)
print(str02)