import re
regex = re.compile(r"""
Profile/(\d+) # Profile followed by digits
(?:(?!Allan)[\S\s])+ # any character
Allan # Allan literally
(?:(?!AddAnswer)[\S\s])+ # any character
AddAnswer(\d+) # AddAnswer, followed by digits
""", flags=re.VERBOSE)
test_str = ("<h4><a href=\"/Profile/23423423423.html\">@@@@@@</a> </h4> said12:49:32\n"
" </div>\n\n"
" <a href=\"javascript:void(0)\" onclick=\"replyAnswer(@@@@@@@@@@,'GET','');\" class=\"reportLink\">\n"
" report </a>\n"
" </div>\n\n"
" <div class=\"details\">\n"
" <p class=\"content\">\n\n\n"
" Hi there, Allan.\n\n\n\n"
" </p>\n\n"
" <div id=\"AddAnswer123456\"></div>\n"
"<h4><a href=\"/Profile/23423423423.html\">@@@@@@</a> </h4> said12:49:32\n"
" </div>\n\n"
" <a href=\"javascript:void(0)\" onclick=\"replyAnswer(@@@@@@@@@@,'GET','');\" class=\"reportLink\">\n"
" report </a>\n"
" </div>\n\n"
" <div class=\"details\">\n"
" <p class=\"content\">\n\n\n"
" Hi there, Allan.\n\n\n\n"
" </p>\n\n"
" <div id=\"AddAnswer123452343232233\"></div>\n"
"<h4><a href=\"/Profile/23423423423.html\">@@@@@@</a> </h4> said12:49:32\n"
" </div>\n\n"
" <a href=\"javascript:void(0)\" onclick=\"replyAnswer(@@@@@@@@@@,'GET','');\" class=\"reportLink\">\n"
" report </a>\n"
" </div>\n\n"
" <div class=\"details\">\n"
" <p class=\"content\">\n\n\n"
" Hi there, Allan.\n\n\n\n"
" </p>\n\n"
" <div id=\"AddAnswer1234523453245\"></div>\n"
"<h4><a href=\"/Profile/23423423423.html\">@@@@@@</a> </h4> said12:49:32\n"
" </div>\n\n"
" <a href=\"javascript:void(0)\" onclick=\"replyAnswer(@@@@@@@@@@,'GET','');\" class=\"reportLink\">\n"
" report </a>\n"
" </div>\n\n"
" <div class=\"details\">\n"
" <p class=\"content\">\n\n\n"
" Hi there, Allan.\n\n\n\n"
" </p>\n\n"
" <div id=\"AddAnswer1234523453245\"></div>\n"
"<h4><a href=\"/Profile/23423423423.html\">@@@@@@</a> </h4> said12:49:32\n"
" </div>\n\n"
" <a href=\"javascript:void(0)\" onclick=\"replyAnswer(@@@@@@@@@@,'GET','');\" class=\"reportLink\">\n"
" report </a>\n"
" </div>\n\n"
" <div class=\"details\">\n"
" <p class=\"content\">\n\n\n"
" Hi there, Allan.\n\n\n\n"
" </p>\n\n"
" <div id=\"AddAnswer1234523453245\"></div>")
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