# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\[(.*?)\]"
test_str = ("\"The quick, brown fox jumps over a lazy dog. DJs flock by when MTV ax quiz prog. \n"
"Junk MTV quiz graced by fox whelps. [Never gonna ] Bawds jog, flick quartz, vex nymphs. \n"
"[give you up\\n] Waltz, bad nymph, for quick jigs vex! Fox nymphs grab quick-jived waltz. \n"
"Brick quiz whangs jumpy veldt fox. [Never ] Bright vixens jump; [gonna let ] dozy fowl \n"
"quack. Quick wafting zephyrs vex bold Jim. Quick zephyrs blow, vexing daft Jim. Charged \n"
"[you down\\n] fop blew my junk TV quiz. How quickly daft jumping zebras vex. Two driven \n"
"jocks help fax my big quiz. Quick, Baz, get my woven flax jodhpurs! \"Now fax quiz Jack!\" \n"
"my brave ghost pled. [Never ] Five quacking zephyrs jolt my wax bed. [gonna ] Flummoxed \n"
"by job, kvetching W. zaps Iraq. Cozy sphinx waves quart jug of bad milk. [run around ] \n"
"A very bad quack might jinx zippy fowls. Few quips galvanized the mock jury box. Quick \n"
"brown dogs jump over the lazy fox. The jay, pig, fox, zebra, and my wolves quack! \n"
"[and desert you] Blowzy red vixens fight for a quick jump. Joaquin Phoenix was gazed \n"
"by MTV for luck. A wizard’s job is to vex chumps quickly in fog. Watch \"Jeopardy!\", \n"
"Alex Trebek's fun TV quiz game.\"")
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