# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\(PERMBLOCK\)\s(?<IP>[.0-9]*)\s\((?<country>.{2}).*? - (?<date>.*)"
test_str = ("180.76.15.158 # lfd: (PERMBLOCK) 180.76.15.158 (CN/China/baiduspider-180-76-15-158.crawl.baidu.com) has had more than 4 temp blocks in the last 86400 secs - Mon Jun 11 05:20:35 2018\n"
"80.211.140.69 # lfd: (PERMBLOCK) 80.211.140.69 (IT/Italy/host69-140-211-80.serverdedicati.aruba.it) has had more than 4 temp blocks in the last 86400 secs - Mon Jun 11 05:39:16 2018\n"
"94.65.235.31 # lfd: (PERMBLOCK) 94.65.235.31 (GR/Greece/ppp-94-65-235-31.home.otenet.gr) has had more than 4 temp blocks in the last 86400 secs - Mon Jun 11 18:40:52 2018\n"
"94.71.194.156 # lfd: (PERMBLOCK) 94.71.194.156 (GR/Greece/athedsl-4511380.home.otenet.gr) has had more than 4 temp blocks in the last 86400 secs - Tue Jun 12 02:24:44 2018\n"
"114.207.112.188 # lfd: (PERMBLOCK) 114.207.112.188 (KR/Republic of Korea/114-207-112-188.tongkni.co.kr) has had more than 4 temp blocks in the last 86400 secs - Tue Jun 12 12:12:10 2018\n"
"94.64.71.183 # lfd: (PERMBLOCK) 94.64.71.183 (GR/Greece/ppp-94-64-71-183.home.otenet.gr) has had more than 4 temp blocks in the last 86400 secs - Wed Jun 13 03:54:37 2018\n"
"89.210.18.91 # lfd: (PERMBLOCK) 89.210.18.91 (GR/Greece/ppp089210018091.access.hol.gr) has had more than 4 temp blocks in the last 86400 secs - Wed Jun 13 04:07:48 2018\n"
"94.65.149.72 # lfd: (PERMBLOCK) 94.65.149.72 (GR/Greece/ppp-94-65-149-72.home.otenet.gr) has had more than 4 temp blocks in the last 86400 secs - Fri Jun 15 05:22:46 2018\n"
"94.64.70.87 # lfd: (PERMBLOCK) 94.64.70.87 (GR/Greece/ppp-94-64-70-87.home.otenet.gr) has had more than 4 temp blocks in the last 86400 secs - Sun Jun 17 07:06:11 2018\n"
"94.64.66.189 # lfd: (PERMBLOCK) 94.64.66.189 (GR/Greece/ppp-94-64-66-189.home.otenet.gr) has had more than 4 temp blocks in the last 86400 secs - Mon Jun 18 00:28:35 2018\n"
"180.76.15.6 # lfd: (PERMBLOCK) 180.76.15.6 (CN/China/baiduspider-180-76-15-6.crawl.baidu.com) has had more than 4 temp blocks in the last 86400 secs - Tue Jun 19 22:15:29 2018\n"
"94.65.145.144 # lfd: (PERMBLOCK) 94.65.145.144 (GR/Greece/ppp-94-65-145-144.home.otenet.gr) has had more than 4 temp blocks in the last 86400 secs - Wed Jun 20 01:13:08 2018\n"
"180.76.15.145 # lfd: (PERMBLOCK) 180.76.15.145 (CN/China/baiduspider-180-76-15-145.crawl.baidu.com) has had more than 4 temp blocks in the last 86400 secs - Thu Jun 21 13:55:36 2018\n"
"2.84.214.95 # lfd: (PERMBLOCK) 2.84.214.95 (GR/Greece/ppp-2-84-214-95.home.otenet.gr) has had more than 4 temp blocks in the last 86400 secs - Thu Jun 21 20:39:07 2018")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html