# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(above|below|title|data[0-9]*|header[0-9]*|caption|above|label[0-9]*) *= +([^{}|\n]+)[^| \n]"
test_str = ("{{Infobox\n"
" |name = {{subst:PAGENAME}}\n"
" |bodystyle = \n\n"
" |titlestyle = \n"
" |abovestyle = background:#cfc;\n"
" |subheaderstyle =\n"
" |title = Test Infobox\n"
" |above = Above text\n"
" |subheader = Subheader above image\n"
" |subheader2 = Second subheader\n\n"
" |imagestyle = \n"
" |captionstyle = \n"
" | image = [[File:example.png|200px|alt=Example alt text]]\n"
" |caption = Caption displayed below example.png\n\n"
" |headerstyle = background:#ccf;\n"
" |labelstyle = background:#ddf;\n"
" |datastyle = \n\n"
" |header1 = Header defined alone\n"
" | label1 = \n"
" | data1 = \n"
" |header2 = \n"
" | label2 = Label defined alone does not display (needs data, or is suppressed)\n"
" | data2 = \n"
" |header3 =\n"
" | label3 = \n"
" | data3 = Data defined alone\n"
" |header4 = All three defined (header, label, data, all with same number)\n"
" | label4 = does not display (same number as a header)\n"
" | data4 = does not display (same number as a header)\n"
" |header5 =\n"
" | label5 = Label and data defined (label)\n"
" | data5 = Label and data defined (data)\n\n"
" |belowstyle = background:#ddf;\n"
" |below = Below text\n"
"}}\n\n"
"{{Infobox\n"
" | title = Top level title\n"
" | header1 = {{Infobox | decat = yes | child = yes\n"
" | title = First subsection\n"
" | label1= Label 1.1\n"
" | data1 = Data 1.1\n"
" }}\n"
" | header2 = {{Infobox | decat = yes | child = yes\n"
" | title = Second subsection\n"
" | label1= Label 2.1\n"
" | data1 = Data 2.1\n"
" }}\n"
" | belowstyle = \n"
" | below = Below text\n"
"}}\n\n"
"{{Infobox\n"
" |name = {{subst:PAGENAME}}\n"
" |bodystyle = width:20em\n\n"
" |titlestyle = \n"
" |title = Test Infobox\n\n"
" |headerstyle = \n"
" |labelstyle = width:33%\n"
" |datastyle = \n\n"
" |header1 = \n"
" | label1 = Label 1\n"
" | data1 = Data 1\n"
" |header2 = \n"
" | label2 = Label 2\n"
" | data2 = Data 2\n"
" |header3 = \n"
" | label3 = Label 3\n"
" | data3 = Data 3\n"
" |header4 = Header 4\n"
" | label4 = \n"
" | data4 = \n"
" |header5 = \n"
" | label5 = Label 5\n"
" | data5 = Data 5: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n\n"
" |belowstyle = \n"
" |below = Below text\n"
"}}\n\n\n"
"{{Infobox\n"
" |name = {{subst:PAGENAME}}\n"
" |bodystyle = width:20em\n\n"
" |titlestyle = \n"
" |title = Test Infobox\n\n"
" |headerstyle = \n"
" |labelstyle = width:33%\n"
" |datastyle = \n\n"
" |header1 = \n"
" | label1 = Label 1\n"
" | data1 = Data 1\n"
" |header2 = \n"
" | label2 = Label 2\n"
" | data2 = Data 2\n"
" |header3 = \n"
" | label3 = Label 3\n"
" | data3 = Data 3\n"
" |header4 = Header 4\n"
" | label4 = \n"
" | data4 = \n"
" |header5 = \n"
" | label5 = Label 5\n"
" | data5 = Data 5: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n\n"
" |belowstyle = \n"
" |below = Below text\n"
"}}\n\n\n"
"{{Infobox\n"
" |name = {{subst:PAGENAME}}\n"
" |bodystyle = width:20em\n\n"
" |titlestyle = \n"
" |title = Test Infobox\n\n"
" |headerstyle = \n"
" |labelstyle = width:33%\n"
" |datastyle = \n\n"
" |header1 = \n"
" | label1 = Label 1\n"
" | data1 = Data 1\n"
" |header2 = \n"
" | label2 = Label 2\n"
" | data2 = Data 2\n"
" |header3 = \n"
" | label3 = Label 3\n"
" | data3 = Data 3\n"
" |header4 = Header 4\n"
" | label4 = \n"
" | data4 = \n"
" |header5 = \n"
" | label5 = Label 5\n"
" | data5 = Data 5: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n\n"
" |belowstyle = \n"
" |below = Below text\n"
"}}\n\n\n"
"{{Infobox\n"
" |name = {{subst:PAGENAME}}\n"
" |bodystyle = width:20em\n\n"
" |titlestyle = \n"
" |title = Test Infobox\n\n"
" |headerstyle = \n"
" |labelstyle = width:33%\n"
" |datastyle = \n\n"
" |header1 = \n"
" | label1 = Label 1\n"
" | data1 = Data 1\n"
" |header2 = \n"
" | label2 = Label 2\n"
" | data2 = Data 2\n"
" |header3 = \n"
" | label3 = Label 3\n"
" | data3 = Data 3\n"
" |header4 = Header 4\n"
" | label4 = \n"
" | data4 = \n"
" |header5 = \n"
" | label5 = Label 5\n"
" | data5 = Data 5: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n\n"
" |belowstyle = \n"
" |below = Below text\n"
"}}\n\n\n"
"{{Infobox\n"
" | headerstyle = background-color:#eee;\n"
" | labelstyle = background-color:#eee;\n"
" | header1 = Main 1\n"
" | header2 = Main 2\n"
" | data3 = {{Infobox | subbox = yes\n"
" | headerstyle = background-color:#ccc;\n"
" | labelstyle = background-color:#ddd;\n"
" | header1 = Sub 3-1\n"
" | header2 = Sub 3-2\n"
" | label3 = Label 3-3 | data3 = Data 3-3\n"
" }}\n"
" | data4 = {{Infobox | subbox = yes\n"
" | labelstyle = background-color:#ccc;\n"
" | label1 = Label 4-1 | data1 = Data 4-1\n"
" }}\n"
" | label5 = Label 5 | data5 = Data 5\n"
" | header6 = Main 6\n"
"}}\n\n\n"
"{{Infobox\n"
" | headerstyle = background-color:#eee;\n"
" | labelstyle = background-color:#eee;\n"
" | header1 = Main 1\n"
" | header2 = Main 2\n"
" | data3 = {{Infobox | subbox = yes\n"
" | headerstyle = background-color:#ccc;\n"
" | labelstyle = background-color:#ddd;\n"
" | header1 = Sub 3-1\n"
" | header2 = Sub 3-2\n"
" | label3 = Label 3-3 | data3 = Data 3-3\n"
" }}\n"
" | data4 = {{Infobox | subbox = yes\n"
" | labelstyle = background-color:#ccc;\n"
" | label1 = Label 4-1 | data1 = Data 4-1\n"
" }}\n"
" | label5 = Label 5 | data5 = Data 5\n"
" | header6 = Main 6\n"
"}}\n\n\n\n")
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