简体   繁体   中英

How to get python to search for whole numbers in a string-not just digits

Okay please do not close this and send me to a similar question because I have been looking for hours at similar questions with no luck.

Python can search for digits using re.search([0-9])

However, I want to search for any whole number. It could be 547 or 2 or 16589425. I don't know how many digits there are going to be in each whole number.

Furthermore I need it to specifically find and match numbers that are going to take a form similar to this: 1005.2.15 or 100.25.1 or 5.5.72 or 1102.170.24 etc.

It may be that there isn't a way to do this using re.search but any info on what identifier I could use would be amazing.

Just use

import re

your_string = 'this is 125.156.56.531 and this is 0540505050.5 !'
result = re.findall(r'\d[\d\.]*', your_string)
print(result)

output

['125.156.56.531', '0540505050.5']

Assuming that you're looking for whole numbers only, try re.search(r"[0-9]+")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM