Python程序教程

您现在的位置是:首页 >  Python

当前栏目

Python 正则匹配数字

Python,正则,匹配,数字
2025-04-01 16:27:57 时间

大家好,又见面了,我是你们的朋友全栈君。电话号码:\d{3}-\d{8}|\d{4}-\d{7}

QQ号:[1-9][0-9]{4,}

中国邮政编码:[1-9]\d{5}(?!\d)

身份证:\d{15}|\d{18}

ip地址:\d+\.\d+\.\d+\.\d+

[1-9]\d*  正整数 -[1-9]\d*   负整数 -?[1-9]\d* 整数 [1-9]\d*|0  非负整数 -[1-9]\d*|0   非正整数 [1-9]\d*\.\d*|0\.\d*[1-9]\d*   正浮点数 -([1-9]\d*\.\d*|0\.\d*[1-9]\d*)  负浮点数 -?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)

匹配价格,并输出平均价格

import re

price='25.34-34.55'

test=re.compile(r'[1-9]\d*\.\d*|0\.\d*[1-9]|[1-9]\d*').findall(price)[0]
test2=re.compile(r'-[1-9]\d*\.\d*|-0\.\d*[1-9]|-[1-9]\d*').findall(price)[0]

i=float(test)
x=-float(test2)
r=(x+i)/2
print r

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/147906.html原文链接:https://javaforall.cn