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

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

返回的结果是打印出的完整行列表,并显示我的else WRONG LINE消息。

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

请指出我的错误。

===============>>#1 票数: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

===============>>#2 票数: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

  ask by user1935007 translate from so

未解决问题?本站智能推荐: