python重新匹配ip地址
我有通过ip地址进行组合的简单脚本。我想从以下输出中正则化ippython重新匹配ip地址,python,re,Python,Re,我有通过ip地址进行组合的简单脚本。我想从以下输出中正则化ip Starting Nmap 7.91 ( https://nmap.org ) at 2020-12-11 15:04 EST Nmap scan report for host.com (127.0.0.1) Host is up (0.14s latency). 我尝试使用此工具:。我能够得到一个符合以下模式的匹配 (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) 但是,此代码返回0个匹配项 rege
Starting Nmap 7.91 ( https://nmap.org ) at 2020-12-11 15:04 EST
Nmap scan report for host.com (127.0.0.1)
Host is up (0.14s latency).
我尝试使用此工具:。我能够得到一个符合以下模式的匹配
(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})
但是,此代码返回0个匹配项
regex = re.match("(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})",output)
print(regex)
预期输出应为127.0.0.1
。对此的任何帮助都将不胜感激。匹配给定字符串开头的模式。看起来您想要的是或:
您应该使用
re.search
>>> import re
>>> text = 'Nmap scan report for host.com (127.0.0.1)'
>>> re.search(r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}", text).group()
'127.0.0.1'
使用re.match,您可以按如下方式使用它:
output = 'Your output'
ip = re.match(r'\(([\d\.]*)\)').groups()[0]
解释:re.match将返回一个类,该类由括号包围,并且只有“.”和数字。然后,groups()将返回所有匹配的组。索引[0]处的字符串将与之匹配。像这样尝试r“^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$”用此正则表达式替换正则表达式这是否回答了您的问题?
output = 'Your output'
ip = re.match(r'\(([\d\.]*)\)').groups()[0]