简体   繁体   English

如何让 python 搜索字符串中的整数,而不仅仅是数字

[英]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]) Python 可以使用 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.它可能是 547 或 2 或 16589425。我不知道每个整数中有多少位数字。

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.此外,我需要它来专门查找和匹配将采用类似于以下形式的数字:1005.2.15 或 100.25.1 或 5.5.72 或 1102.170.24 等。

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.可能没有办法使用 re.search 来做到这一点,但任何关于我可以使用什么标识符的信息都会很棒。

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 output

['125.156.56.531', '0540505050.5']

Assuming that you're looking for whole numbers only, try re.search(r"[0-9]+")假设您只查找整数,请尝试re.search(r"[0-9]+")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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