RegEx (26) - 使用[ ][ ] 來找在範圍內的兩位數
阿新 • • 發佈:2021-01-17
使用python來做正則表示式:
import re
txt = "2343 33 89 2 sdf"
#Check if the string has any two-digit numbers, from 00 to 59:
x = re.findall("[0-5][0-9]", txt)
print('numbers from 00 to 59 結果: ',x)
if x:
print("Yes, there is at least one match!")
else :
print("No match")
txt_new = "234 33 89 2 sdf"
#Check if the string has any two-digit numbers, from 00 to 59:
y = re.findall('[0-5][0-9]', txt_new)
print('numbers from 00 to 59 結果:',y)
[0-5][0-9]意思: Returns a match for any two-digit numbers from 00 and 59
結果如下:
解釋一下結果:由於程式碼中用了[ ][ ]來做範圍。比如[0-5]對應的是十位數所取的數字的範圍為0-5,[0-9] 表示個位數的數字範圍為0-9.
如果覺得不錯,就點贊或者關注或者留言~
謝謝~