# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\[(?P<key>.+?)(?:\s+(?P<settings>.*?))?\]"
test_str = ("### The I_Soil System\n\n"
"The I-soil material model is a nonlinear hysteretic soil model that is based on the distributed\n"
"element models developed by [!citet](iwan1967on) and [!citet](chiang1994anew). In 1-D, this model takes\n"
"the backbone stress-strain curve and divides it into a set of elastic-perfectly plastic curves. The\n"
"total stress then is the sum of the stresses from the individual elastic-perfectly plastic curves.\n\n"
"The three dimensional generalization of this model is achieved using von-Mises failure criteria for\n"
"each elastic-perfectly plastic curve resulting in an invariant yield surfaces in three-dimensional\n"
"stress space like in [fig:yieldsurface] (after [!citet](chiang1994anew)).")
matches = re.finditer(regex, test_str, re.MULTILINE | re.DOTALL | re.UNICODE)
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