python - 包含正则表达式匹配的打印行和列表

时间:2016-05-24 18:00:18

标签: python regex ip-address ipv4

我有一个日志文件,至少有一千行

abc.txt:
1. example eg, ham, cheese 350.122.345.8
2. cheese ham eg, example 231.242.1.2
3. Ham cheese, example,e.g 100.200.100.200
4.
5. Ham cheese, example,e.g 100.200.100.200
1000. 

我想要的最终结果:

仅打印与ip地址范围内的数字匹配的行。因此它应该只打印:

2. cheese ham eg, example 231.242.1.2
3. Ham cheese, example,e.g 100.200.100.200
5. Ham cheese, example,e.g 100.200.100.200

我尝试了以下代码但无法获得我想要的结果:

import re

txt=open('/sdcard/Download/abc.txt','r')

pattern=re.compile('(^[2][0-5][0-5]|^[1]{0,1}[0-9]{1,2})\.([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})\.([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})\.([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})$', re.DOTALL)

for line in txt:
    if str(pattern) in line:
        print line
    else:
        print 'WRONG LINE:',line

返回的结果是打印出的完整行列表并显示我的其他错误行消息。

我使用在线检查器检查了我的正则表达式,它显示了正确的行为,匹配所有ipv4地址,不超过.255

请指出我的错误。

2 个答案:

答案 0 :(得分:0)

以下是更正后的正则表达式和代码:

import re

txt= {"1. example eg, ham, cheese 350.122.345.8",
      "2. cheese ham eg, example 231.242.1.2",
      "3. Ham cheese, example,e.g 100.200.100.200",
      "4.",
      "5. Ham cheese, example,e.g 100.200.100.200"}


pattern=re.compile('([2][0-5][0-5]|[1]{0,1}[0-9]{1,2})\.([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})\.([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})\.([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})', re.DOTALL)

for line in txt:
    if pattern.search(line):
        print line
    else:
        print 'WRONG LINE:',line

答案 1 :(得分:0)

来自:@RudyTheHunter

import re

txt=open('/sdcard/Download/abc.txt','r')

pattern=re.compile('([2][0-5][0-5]|[1]{0,1}[0-9]{1,2})\.([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})\.([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})\.([0-2][0-5][0-5]|[1]{0,1}[0-9]{1,2})', re.DOTALL)

for line in txt:
    if pattern.search(line):
        print line
    else: 
        print 'WRONG LINE:',line