本文介绍了提取与IP地址的字符串匹配的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python来查找与IP地址的特定模式匹配的行.

I am working on python to find a line that matches a particular pattern of an IP address.

f = open('file.txt','r')
for line in f:
    if line.find("N2:42.61.0.69")
        print line

我要匹配的模式是"node number":"IP address",其中"node number"是字母"N",后跟数字".例如N23,N456,N98765等.

The pattern that I am trying to match is "node number":"IP address", where"node number" is the alphabet 'N' followed by a 'number'. Such as N23, N456, N98765, etc.

我使用了模式N2:42\.61\.0\.69,但是没有产生任何结果.

I used the pattern N2:42\.61\.0\.69, but it didn't yield any result.

大多数示例都讨论正则表达式以匹配特定模式,例如IP地址"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$".

Most of the examples talks about regex to match a particular pattern such as for an IP address "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$".

但是在这里,我要将特定字符串与IP地址匹配.感谢您的帮助!

But here I want to match a particular string with IP address.Thanks for the help!

推荐答案

函数find():

因此,如果要查找包含给定字符串(在您的情况下为"N2:42.61.0.69")的行,则条件应为:

So if you want to find lines containing the given string( "N2:42.61.0.69" in your case), the condition should be:

if line.find("N2:42.61.0.69") != -1:

这篇关于提取与IP地址的字符串匹配的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:12