import re
regex = re.compile(r"\[!(?P<command>\w+)(?:!(?P<subcommand>\w+))? *(?P<settings>\w+=.*?)?\](?:\((?P<inline>.*?)\))", flags=re.MULTILINE)
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 = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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