欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 正則表達式匹配數(shù)字及字符串中的純數(shù)字

 更新時間:2019年08月05日 11:39:51   作者:Watch_dou  
這篇文章主要介紹了Python 正則表達式匹配數(shù)字及使用正則表達式找出字符串中的純數(shù)字,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

Python 正則表達式匹配數(shù)字

電話號碼:\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*      正整數(shù)
-[1-9]\d*   負整數(shù)
-?[1-9]\d* 整數(shù)
[1-9]\d*|0  非負整數(shù)
-[1-9]\d*|0   非正整數(shù)
[1-9]\d*\.\d*|0\.\d*[1-9]\d*$   正浮點數(shù)
-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$  負浮點數(shù)
-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$  浮點數(shù)

匹配價格,并輸出平均價格

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

知識點擴展:python 正則表達式找出字符串中的純數(shù)字

1、簡單的做法

>>> import re
>>> re.findall(r'\d+', 'hello 42 I'm a 32 string 30')
['42', '32', '30']

然而,這種做法使得字符串中非純數(shù)字也會識別

>>> re.findall(r'\d+', "hello 42 I'm a 32 str12312ing 30")
['42', '32', '12312', '30']

2、識別純數(shù)字

如果只需要用單詞邊界( 空格,句號,逗號) 分隔的數(shù)字,你可以使用 \b

>>> re.findall(r'\b\d+\b', "hello 42 I'm a 32 str12312ing 30")
['42', '32', '30']
>>> re.findall(r'\b\d+\b', "hello,42 I'm a 32 str12312ing 30")
['42', '32', '30']
>>> re.findall(r'\b\d+\b', "hello,42 I'm a 32 str 12312ing 30")
['42', '32', '30']

總結(jié)

以上所述是小編給大家介紹的Python 正則表達式匹配數(shù)字及字符串中的純數(shù)字,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

最新評論