1 import re 2 3 zen = "Arizona 479, 501, 870. Carlifornia 209, 213, 650." 4 5 m = re.findall("\d+", zen) 6 7 print(m)
['479', '501', '870', '209', '213', '650']
可是上述這種方式也會引入非純數據,例子以下:spa
1 import re 2 3 zen = "Arizona 479, 501, 870. Carlifornia 209, 213, 650. string666xxx." 4 5 m = re.findall("\d+", zen) 6 7 print(m)
['479', '501', '870', '209', '213', '650', '666']
1 import re 2 3 zen = "Arizona 479, 501, 870. Carlifornia 209, 213, 650. string666xxx." 4 5 m = re.findall(r"\b\d+\b", zen) 6 7 print(m)
['479', '501', '870', '209', '213', '650']
第一種狀況表達式開頭有沒有 r 影響不大,第二種狀況只有加了 r 才能使用\b(表明先後均有空格的純數字), 不然只會返回一個空列表code