import re
regex = re.compile(r"(?:[!]\[(?P<caption>.*?)\])\((?P<image>.*?)\)", flags=re.MULTILINE)
test_str = ("This is a regex to detect markdown image links of the form \n\n"
"Here are some markdown links\n"
"- [pandoc-fignos](https://github.com/tomduck/pandoc-fignos): Numbers figures and figure references.\n"
"The images section will walk you through how to add and reference images so that the pandoc system can properly number them. For example, this [figure](./ch0_1_images.md#fig:ch0_1_images-1) illustrates a VOD curve for a packaged watergel explosive and this [figure](./ch0_1_images.md#fig:ch0_1_images-2) depicts a circular arc.\n"
"## [Equations](./ch0_2_equations.md#sec:ch0_2_equations-1)\n"
"The equations section will discuss how to use equations and reference them properly. See the [internal energy equation](./ch0_2_equations.md#eq:ch0_2_equations-1) or the [detonation pressure](./ch0_2_equations.md#eq:ch0_2_equations-2)\n"
"This string does not contain any links\n\n"
"Here are some image links:\n"
"{#fig:id}\n"
"{#fig:id tag=\"B.1\"}\n"
"{#fig:ch0_1_images-1 width=100%}\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"")
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