import re
regex = re.compile(r"^(?!.*://)(?P<md>.*\.[mM][dD])?(?P<section>#.*)?$", flags=re.MULTILINE)
test_str = ("We want to find relative markdown urls, we don't care about https or ftp:// type urls...\n"
"^(?:(?!(?:https?|ftp)://).*)(?P<md>\\.[mM][dD])(?P<section>#?.*)$\n"
"https://github.com/tomduck/pandoc-fignos\n"
"http://github.com/tomduck/pandoc-fignos\n"
"ftp://github.com/tomduck/pandoc-fignos\n"
"ftp://github.com/ tomduck/ pandoc-fignos\n"
"ftp:// github.com/ tomduck/ pandoc-fignos\n"
"ftps://github.com/tomduck/pandoc-fignos\n"
"www.google.ca\n"
"google.com\n"
"./ch0_1_images.md#fig:ch0_1_images-1\n"
"./ch0_1_images.md#fig:ch0_1_images-2\n"
"./ch0_2_equations.md#sec:ch0_2_equations-1\n"
"./ch0_2_equations.md#eq:ch0_2_equations-1\n"
"./ch0_2_equations.md#eq:ch0_2_equations-2\n"
"./ch0_2_equations.md\n"
"./hello world.md\n"
"#eq:ch0_2_equations-2\n"
"#eq:ch0_2_equations-2")
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