# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(lease\s(?P<ip>[0-9\.]+)\s{.+})"
test_str = ("lease 10.31.31.131 {\n"
" starts 0 2014/08/31 14:54:11;\n"
" ends 0 2014/08/31 15:04:11;\n"
" cltt 0 2014/08/31 14:54:11;\n"
" binding state active;\n"
" next binding state free;\n"
" rewind binding state free;\n"
" hardware ethernet d0:e7:82:19:8e:ec;\n"
" set ddns-rev-name = \"131.31.31.10.in-addr.arpa.\";\n"
" set ddns-txt = \"0044522956d8e56b0f40798743a0660994\";\n"
" set ddns-fwd-name = \"Chromecast.example.\";\n"
" client-hostname \"Chromecast\";\n"
"}\n"
"lease 10.31.31.133 {\n"
" starts 0 2014/08/31 14:54:51;\n"
" ends 0 2014/08/31 15:04:51;\n"
" cltt 0 2014/08/31 14:54:51;\n"
" binding state active;\n"
" next binding state free;\n"
" rewind binding state free;\n"
" hardware ethernet 00:18:0a:22:2f:9f;\n"
" uid \"\\001\\000\\030\\012\\\"/\\237\";\n"
" set ddns-rev-name = \"133.31.31.10.in-addr.arpa.\";\n"
" set ddns-txt = \"31897a4f3a66a2733b86a7cd3b0b0b2923\";\n"
" set ddns-fwd-name = \"ap-1-00180a222f9f.example.\";\n"
" client-hostname \"ap-1-00180a222f9f\";\n"
"}\n")
matches = re.search(regex, test_str, re.MULTILINE | re.DOTALL)
if matches:
print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group()))
for groupNum in range(0, len(matches.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.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