import re
regex = re.compile(r"(\d.+)(?=\s*\" Unit=\"W\" Type=\"AC_Power\")", flags=re.MULTILINE)
test_str = ("<Measurements>\n"
"<Measurement Value=\"237.6\" Unit=\"V\" Type=\"AC_Voltage\"/>\n"
"<Measurement Value=\"9.828\" Unit=\"A\" Type=\"AC_Current\"/>\n"
"<Measurement Value=\"2344.7\" Unit=\"W\" Type=\"AC_Power\"/>\n"
"<Measurement Value=\"2343.4\" Unit=\"W\" Type=\"AC_Power_fast\"/>\n"
"<Measurement Value=\"50.003\" Unit=\"Hz\" Type=\"AC_Frequency\"/>\n"
"<Measurement Value=\"294.4\" Unit=\"V\" Type=\"DC_Voltage1\"/>\n"
"<Measurement Value=\"171.3\" Unit=\"V\" Type=\"DC_Voltage2\"/>\n"
"<Measurement Value=\"8.056\" Unit=\"A\" Type=\"DC_Current1\"/>\n"
"<Measurement Unit=\"A\" Type=\"DC_Current2\"/>\n"
"<Measurement Value=\"343.8\" Unit=\"V\" Type=\"LINK_Voltage\"/>\n"
"<Measurement Unit=\"W\" Type=\"GridPower\"/>\n"
"<Measurement Unit=\"W\" Type=\"GridConsumedPower\"/>\n"
"<Measurement Unit=\"W\" Type=\"GridInjectedPower\"/>\n"
"<Measurement Unit=\"W\" Type=\"OwnConsumedPower\"/>\n"
"<Measurement Value=\"100.0\" Unit=\"%\" Type=\"Derating\"/>\n"
"</Measurements>\n"
"</Device>\n"
"</root>")
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