หากต้องการดึงตัวเลข/หลักตัวเลข แยกกันทีละหลัก จากข้อความ ใช้ regex:
ตัวอย่าง
import re
s = '12345 abcdf 67'
result = re.findall(r'\d', s)
print(result)
ผลลัพธ์
['1', '2', '3', '4', '5', '6', '7']
หากต้องการดึง กลุ่มตัวเลข (ต่อเนื่อง) ใช้ regex:
ตัวอย่าง
import re
s = '12345 abcdf 67'
result = re.findall(r'\d+', s)
print(result)
ผลลัพธ์
['12345', '67']