简体   繁体   English

如何从字符串中获取第一个IP地址

[英]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.

相关问题 如何在 Python 中从 ping 中获取 IP 地址 - How to Grab IP address from ping in Python 正则表达式:如何从字符串中仅提取第一个IP地址(在Python中) - Regex: how to extract only first IP address from string (in Python) 如何从Python中的IP地址获取NAPTR记录? - How do I grab a NAPTR record from an IP address in Python? 如何从字符串中提取 IP 地址 - How to extract IP address from the string 如何从HTML字符串中提取IP地址? - How to extract an IP address from an HTML string? 如何从netstat抓取字符串(ip)管道将字符串(ip)传递给whois命令,如何从whois输出抓取字符串(国家/地区)。 并使用iptables禁止ip - How to grab string(ip) from netstat pipe the string(ip) to whois command, grab a string(country) from whois output. And use iptables to ban the ip 如何从字典键“Message”中提取第一个 IP 地址,并使用提取的 IP 地址添加一个名为“IP”的新键? - How to extract the first IP address from a dictionary key “Message” and add a new key called “IP” with the extracted IP address? 如何获取字符串python中的前x个字符 - how to grab the first x characters in a string python 使用 Python 获取 IP 地址中的最后一个八位字节 - Grab the last octet in an IP address using Python 如何使用 Python 从远程 PHP 响应字符串中获取第一行 url? - How to grab first line url from a remote PHP response string using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM