import re
uom_regex = 'box(?:es)?|bxs|bags?'
regex = r'(?=({0}))\1([a-zA-Z]+)'.format(uom_regex)
test_strings = ["boxes", "bags", "boxesapple", "boxapple", "bagapple", 'bagsapple']
for test_string in test_strings:
result = re.sub(regex, r'\1 \2', test_string)
print(f"Original: {test_string}")
print(f"Modified: {result}\n")
aW1wb3J0IHJlCgp1b21fcmVnZXggPSAnYm94KD86ZXMpP3xieHN8YmFncz8nCnJlZ2V4ID0gcicoPz0oezB9KSlcMShbYS16QS1aXSspJy5mb3JtYXQodW9tX3JlZ2V4KQoKdGVzdF9zdHJpbmdzID0gWyJib3hlcyIsICJiYWdzIiwgImJveGVzYXBwbGUiLCAiYm94YXBwbGUiLCAiYmFnYXBwbGUiLCAnYmFnc2FwcGxlJ10KCmZvciB0ZXN0X3N0cmluZyBpbiB0ZXN0X3N0cmluZ3M6CiAgICByZXN1bHQgPSByZS5zdWIocmVnZXgsIHInXDEgXDInLCB0ZXN0X3N0cmluZykKICAgIHByaW50KGYiT3JpZ2luYWw6IHt0ZXN0X3N0cmluZ30iKQogICAgcHJpbnQoZiJNb2RpZmllZDoge3Jlc3VsdH1cbiIp