Reputation: 59
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:
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:
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/
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. What am I doing wrong and why is re.findall not accepting my tested regex?
Upvotes: 2
Views: 673
Reputation: 1056
could you be just matching the first line ? try adding /m flag to your regex
pattern = re.compile("whatever",re.MULTILINE)
also note that if you are matching a pattern with groups in it findall returns list of lists
Upvotes: 0
Reputation: 12371
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
.
Maybe you want to print all matches? You can do that with
print '\n'.join(matches)
Upvotes: 1
Reputation: 66775
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)
Upvotes: 6