import re
regex = re.compile(r"<[a-z\s]+id=[\'\"]mobile[\w]+[\'\"][\sa-zA-Z\d\'\=\;\:]*>([a-zA-Z\d\s]+)<[\/a-z\s]+>")
test_str = ("<div id='mobileBody'>Message</div>\n"
"<div id=\"mobiletitle\" >Message</div>\n"
"<div id='mobileBody' style='display:none;'>Message</div>\n"
"<div id='mobileTitle' style='display:none;'>Message</div>\n"
"<div id=\"title\" >asd</div>\n"
"<div id=\"title\">asd</div>")
subst = "$1"
result = regex.sub(subst, test_str)
if result:
print(result)
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