import re
regex = re.compile(r"\(pp\.\s+\d+(?:-\d+)?\)|\b\d+(?:-\d+)?(?=(?:\s*,\s*\d+(?:-\d+)?)*\.)", flags=re.MULTILINE)
test_str = ("- Mitchell, J.A. (2017). Citation: Why is it so important. Mendeley Journal, 67(2), (pp. 81-95). \n\n"
"- Denhart, H. (2008). Deconstructing barriers: Perceptions of students labeled with learning disabilities in higher education. Journal of Learning Disabilities, 40,41, 483-497.\n\n"
"(pp. 81). \n"
"12-12\n"
"http://test.com/12-23\n\n"
"Usually the page numbers follow a commas and then there is a dot (like this: , 1-2. ) How can I change the code according to this? Same goes for when there is only one page listed , number. and the ` (pp. 12)` format.")
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