Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเขียน Python Regex จับคู่เลขทศนิยม (Floating Point Numbers)

ใช้ Python regex จับคู่ floating point numbers

ตัวอย่าง

import re
s = '234.6789'
match = re.match(r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?', s)
print(match.group())

s2 = '0.45'
match = re.match(r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?', s2)
print(match.group())

ผลลัพธ์

234.6789
0.45