import re
regex = re.compile(r"(?<=\|)(?!\s).*?(?!\s)(?=\|)")
test_str = "| --------- | -------- | --------- | |propped| - | -a flashlight in one hand and a large leather-bound book (A History of Magic by Bathilda Bagshot) propped open against the pillow. | |Pointless| - | -“Witch Burning in the Fourteenth Century Was Completely Pointless — discuss.”| |unscrewed| - | -Slowly and very carefully he unscrewed the ink bottle, dipped his quill into it, and began to write,| |downtrodden| - | -For years, Aunt Petunia and Uncle Vernon had hoped that if they kept Harry as downtrodden as possible, they would be able to squash the magic out of him.| |sheets,| - | -As long as he didn’t leave spots of ink on the sheets, the Dursleys need never know that he was studying magic by night.| |flinch| - | -But he hoped she’d be back soon — she was the only living creature in this house who didn’t flinch at the sight of him.|"
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