Hi,
I'm trying to parse an apache log file and match the IP addresses in it. This is my code so far:

import re
ippattern = re.compile(r"\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b")

def matchmyip():
	if ippattern.search(line):
		matchedip = ippattern.search(line)
                print matchedip.group()

Now given a line like this from a log file:

1999-08-01 00:00:00 212.67.129.225 - W3SVC3 PROWWW01 194.128.73.195 GET /entrance/V0_1/default.asp bhcd2=933465599 302 0 497 396 15 80 HTTP/1.1 Mozilla/4.0+(compatible;+MSIE+4.01;+Windows+95;+FREESERVE_IE4) bhCookie=1;+ASPSESSIONIDGGGGGRDN=PPLCJMKDDJBMPIMEAEDPIJGF -

my regular expression only matches the first ip address but not the second one. How should I modify my regex so that it matches multiple occurences of the same pattern in a single line?

Thanks,
Adi

Call the findall method instead of search to get an array of matches.

def matchmyip(line):
    if ippattern.search(line):
        print ippattern.findall(line)

import re
ippattern = re.compile(r"\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b")

if __name__ == "__main__":
    s = "1999-08-01 00:00:00 212.67.129.225 - W3SVC3 PROWWW01 194.128.73.195 GET /entrance/V0_1/default.asp bhcd2=933465599 302 0 497 396 15 80 HTTP/1.1 Mozilla/4.0+(compatible;+MSIE+4.01;+Windows+95;+FREESERVE_IE4) bhCookie=1;+ASPSESSIONIDGGGGGRDN=PPLCJMKDDJBMPIMEAEDPIJGF -"
    matchmyip(s)

Thank you! Silly of me to not go through all the methods for 're' before asking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.