import re
regex = re.compile(r"(<([\w\:\-]+)(.+?)<\/\2>)")
test_str = " <model name=\"user\" label=\"User / Device\"> <field name=\"device_label\" label=\"Device Label\" type=\"text:name\" /> <field name=\"logged_in_as_administrator\" label=\"Logged in as Administrator\" type=\"boolean\" /> <field name=\"logged_in_as_consultant\" label=\"Logged in as Consultant\" type=\"boolean\" /> <field name=\"logged_in_as_contractor\" label=\"Logged in as Contractor\" type=\"boolean\" /> <field name=\"logged_in_as_csd\" label=\"Logged in as CSD\" type=\"boolean\" /> <field name=\"logged_in_as_engineer\" label=\"Logged in as Engineer\" type=\"boolean\" /> <field name=\"logged_in_as_engineer_coordinator\" label=\"Logged in as Operations Coordinator\" type=\"boolean\" /> <field name=\"logged_in_as_procurement\" label=\"Logged in as Procurement\" type=\"boolean\" /> <field name=\"logged_in_as_sda\" label=\"Logged in as SDA\" type=\"boolean\" /> <field name=\"logged_in_as_pm\" label=\"Logged in as Project Manager\" type=\"boolean\" /> <field name=\"app_version\" label=\"App Version\" type=\"text\" /> <field name=\"updated_at\" label=\"Updated At\" type=\"text\" /> <belongs-to model=\"journey_account\" /> <belongs-to model=\"role\" name=\"current_role\" /> <has-many model=\"notification\" name=\"notifications\" /> <display>{device_label}</display> </model> "
match = regex.search(test_str)
if match:
print(f"Match 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