# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"[^\[\] ](?:\[.*?\])*?(\[\|\])(?:\[.*?\])*?[^\[\] ]"
test_str = ("{title:Opw 364 Dm}\n"
"{subtitle:Prijs onze Heer}\n"
"{tag:bidstond}\n"
"{key:Dm}\n"
"{c:Tempo 120}\n"
"{c:Maat 4/4}\n"
"{c:OPW OK}\n\n"
"[Dm][|][Gm][C][|][Dm][|][Dsus][D][|]\n"
"[Gm][|][C7][|][F][|][Asus][A7][|] \n\n"
"[|][Dm]Prijs onze Heer,[|]\n"
"Hij alleen is de [|][C]machtige God, [|]\n"
"vol van majesteit. [|][Bb]Kom en buig neer,[|]\n"
"loof zijn heil’ge [|][A7sus]naam. [|][A7]\n"
"[|]Breng dan aan Hem[|]\n"
"als een offer een [|][C]zoete geur,[|]\n"
"de gebeden van je [|][Bb]hart naar zijn troon.[|]\n"
"Geef Hem dank en [|][A7sus]eer. [|][A7]\n"
"[|][Dm]De Heer almachtig,[|][Gm]\n"
"zijn [C]liefde duurt voor [|][Dm]eeuwig.[|][D]\n"
"Zijn [|][Gm]trou[|][C7]w en goedheid gaan ons [|][F]voor.[|][A7]\n"
"[|][Dm]Zijn kracht zal nimmer[|][Gm] \n"
"[C]falen of ont[|][Dm]breken[|][D]\n"
"en zijn [|][Gm]lie[|][C7]fde [|][F]blijft.\n\n"
"{c:1e keer:}\n"
"[|][Asus] [A] \n\n"
"{c:Lied herhalen}\n\n"
"[|][Bb] [|][F]\n")
matches = re.finditer(regex, test_str, re.MULTILINE)
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