[英]How to grab first ip address from a string
I am trying to grab an ip address
from a string and facing an issue.please help.我正在尝试从字符串中获取
ip address
并遇到问题。请帮忙。
inet addr:11.11.11.11 Bcast:11.11.11.111 Mask:111.111.11.1
. inet addr:11.11.11.11 Bcast:11.11.11.111 Mask:111.111.11.1
。
This is the string I have and I need ip address next to addr:这是我拥有的字符串,我需要在 addr 旁边的 ip 地址:
I have tried the following code and failed to do in python:我尝试了以下代码,但在 python 中失败:
ip = re.findall(r'(?:\\d{1,3}\\.)+(?:\\d{1,3})', line)
and get index 0 item. ip = re.findall(r'(?:\\d{1,3}\\.)+(?:\\d{1,3})', line)
并获得索引 0 的项目。
Result : This is actually giving me nothing in return结果:这实际上没有给我任何回报
Your REGEX could be more specific, I think you could use something like : 您的REGEX可能更具体,我想您可以使用类似:
addr:(?<ip>\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})
In python: 在python中:
match = re.match(r'addr:(?<ip>\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})', line)
You can then access the ip
group by calling match.group('ip')
. 然后,您可以通过调用
match.group('ip')
来访问ip
组。
I noted that your regex will match invalid IPv4 addresses. 我注意到您的正则表达式将匹配无效的IPv4地址。
import re
string = 'inet addr:300.11.11.11 Bcast:11.11.11.111 Mask:111.111.11.1'
# your pattern
ip_address_pattern = re.compile(r'(?:\d{1,3}\.)+(?:\d{1,3})')
find_ip_address = re.findall(ip_address_pattern, string)
if find_ip_address:
print (find_ip_address)
# outputs
['300.11.11.11', '11.11.11.111', '111.111.11.1']
I have used this IPv4_format in the past to extract valid IPv4 addresses. 我过去曾使用过此IPv4_format来提取有效的IPv4地址。
import re
string = 'inet addr:11.11.11.11 Bcast:11.11.11.111 Mask:111.111.11.1'
# Valid IPv4 address format
ip_address_pattern = re.compile(r'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b')
find_ip_address = re.findall(ip_address_pattern, string)
if find_ip_address:
print (find_ip_address)
# outputs
['11.11.11.11', '11.11.11.111', '111.111.11.1']
import re
line = "inet addr:11.11.11.11 Bcast:11.11.11.111 Mask:111.111.11.1"
pattern = r"[\d]{2}[.][\d]{2}[.][\d]{2}[.][\d]{2}[\D]"
re.findall(pattern, line)
['11.11.11.11 ']
re.findall(pattern, line)[0].strip()
'11.11.11.11'
if you have more than one element in the list just run a list-comp using .strip()
如果列表中有多个元素,只需使用
.strip()
运行list-comp
[i.strip() for i in re.findall(pattern, line)]
['11.11.11.11']
re.match()
is not going to work because it will try to match your pattern starting at the beginning of the string (granted that your pattern does not include the " inet addr: " portion.
re.match()
无法正常工作,因为它将尝试从字符串的开头开始匹配您的模式(请注意,您的模式不包含“ inet addr: ”部分。
re.search()
works but it misses recurring elements and only returns the first encounter of the pattern upon a successful match, in addition you'll have to use filter
to extract the element. re.search()
可以工作,但是它会丢失重复出现的元素,并且仅在成功匹配后才返回模式的首次匹配,此外,您还必须使用filter
来提取元素。
finally, the key to solving this problem lies in the last character of your target, xx.xx.xx.xx[\\D]
. 最后,解决此问题的关键在于目标的最后一个字符
xx.xx.xx.xx[\\D]
。 The [\\D]
directive ensures that the pattern looks for a no-integer at index 12, [\\s]
works equally well and it matches a space. [\\D]
指令可确保模式在索引12处查找无整数, [\\s]
同样有效,并且与空格匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.