import re
regex = re.compile(r"\b(k(?:ilogram|g)\.*s*\.*)\W*(?:\h+weight)?(?:\h+(?:is|are))?\W*(\d[0-9.,]*\b)|(\d[0-9.,]*)\W*(k(?:ilogram|g)\.*s*\.*)\b", flags=re.MULTILINE)
test_str = ("15kilograms fs s \n"
"kg 10 fe\n"
"something 10.5 kg-fef\n\n"
"11.55kg gfre\n"
"11.55-kg gfre\n"
"11.55 kg gfre\n"
"kg 11.25 gre\n"
"efewf kg 11 fweew\n"
"ewsfewf kg\n"
"55 few\n"
"kg-55.55 dwd\n"
"eeewegv kg 55.55 ee\n"
"fewfewff 10\n"
"kg feeee\n"
"smth. .15kg ewfcwe\n"
"something kilogram weight is 15.5\n")
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