简体   繁体   English

Python RE“ re,findall”

[英]Python RE “re,findall”

Thank you in advance. 先感谢您。 My question is: 我的问题是:

I have a block of Python code in which I am attempting to use "os.walk,re and re.findall ip" in attempting to find all ip addresses within several files such as: 我有一块Python代码,我试图在其中使用“os.walk,re和re.findall ip”来尝试查找几个文件中的所有IP地址,例如:

file1:192.168.3.1
file1:192.168.3.2
file1:mary had a little lamb
file1:192.168.3.3
file1:192.168.3.11
file1:10.255.3.1

file10:192.168.3.1
file10:192.168.3.2
file10:192.168.3.3
file10:192.168.3.4
file10:192.168.3.11
file10:192.168.1.1
file10:10.255.3.1

file2:192.168.3.1
file2:192.168.3.2
file2:192.168.3.3
file2:192.168.3.4
file2:192.168.3.11
file2:192.168.1.1
file2:10.255.3.1

file3:192.168.3.1
file3:192.168.3.2
file3:192.168.3.3
file3:192.168.3.4
file3:192.168.3.11
file3:192.168.1.1
file3:10.255.3.1

etc etc. My code block 等等等等。我的代码块

for subdir, dirs, files in os.walk('.'):
  for file in files:
    matches = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", open(file, "r").read())
    if matches:
        print "Here is what is inside %s = %s" % (file,matches[0])

What happens is it only lists one particular type of ip such as: 发生的事情是它仅列出一种特定类型的ip,例如:

Here is what is inside file3 = 192.168.3.1
Here is what is inside file6 = 192.168.3.1
Here is what is inside file7 = 192.168.3.1
Here is what is inside file1 = 192.168.3.1
Here is what is inside file9 = 192.168.3.1
Here is what is inside file5 = 192.168.3.1
Here is what is inside file8 = 192.168.3.1
Here is what is inside file10 = 192.168.3.1
Here is what is inside file4 = 192.168.3.1

In thinking that it was my regex was incorrect, I tested it with http://gskinner.com/RegExr/ 考虑到这是我的正则表达式不正确,我用http://gskinner.com/RegExr/对其进行了测试

and the regular expression tested fine with my data that I supplied at the site in that it found everything that was an ip address. 正则表达式对我在网站上提供的数据进行了很好的测试,因为它找到了所有的IP地址。 What am I doing wrong and why is re.findall not accepting my tested regex? 我做错了什么,为什么re.findall不接受我测试的正则表达式?

You are only printing out one match: 您只打印出一个匹配项:

if matches:
    print "Here is what is inside %s = %s" % (file,matches[0])

instead of all of them 而不是所有这些

if matches:
    for match in matches:
        print "Here is what is inside %s = %s" % (file,match)

You are only printing the first match, and - at least for the part of the dataset you've shown - the first entry is always 192.168.3.1 . 您只打印第一个匹配项,并且 - 至少对于您显示的数据集部分 - 第一个条目始终为192.168.3.1

Maybe you want to print all matches? 也许您想打印所有匹配项? You can do that with 你可以这样做

print '\n'.join(matches) 

could you be just matching the first line ? 你能不能匹配第一线? try adding /m flag to your regex 尝试将/ m标志添加到正则表达式中

pattern = re.compile("whatever",re.MULTILINE)

also note that if you are matching a pattern with groups in it findall returns list of lists 另请注意,如果要将模式与其中的组匹配,则findall将返回列表列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM