1. 程式人生 > 其它 >RegEx (24) - 使用[ ] 來檢查string中有沒有數字

RegEx (24) - 使用[ ] 來檢查string中有沒有數字

技術標籤:RegExpython正則表示式regex

使用python來做正則表示式:

import re

txt = "23n sdf 2432 42 sffw"

#Check if the string has any 0, 1, 2, or 3 digits:
x = re.findall("[0123]", txt)
print(x)

if x:
  print("Yes, there is at least one match!")
else:
  print("No match")

字元[0123]: Returns a match where any of the specified digits (0, 1, 2, or 3) are present

結果如下:
在這裡插入圖片描述
如果覺得不錯,就點贊或者關注或者留言~
謝謝~