I'm trying to construct a regular expression to capture units and the corresponding values.
For example,
import re
candis = ['mmol','mm']
test_reg = '|'.join([ut+r"\-?[1-4]?" for ut in candis])
test_reg = r"\b(?:" + test_reg + r")\b"
test_reg = r"\d (?:" + test_reg + r"\s?){1,3}"
test_str = '3 mmol mm'
re.findall(test_reg,test_str)
the test_reg is constructed to capture the unit mmol mm and the corresponding value of 3.
However, as you can readily observe in the example, test_reg does not work for a string like 3 mmol2mm because of the \b.
How can I construct a regular expression that can also match 3 mmol2mm and 3 mmolmm, which only contains word combinations that are strictly from candis? (3 mmol mmb won't match)