fork download
  1. import re
  2.  
  3. candis = ['mmol','mm']
  4. test_reg = r"\d+(?=((?:\s*(?:{})-?[1-4]?){{1,3}}))\1\b".format('|'.join(candis))
  5. test_str = '3 mmol mm 3 mmol2mm and 3 mmolmm AND NOT 3 mmol mmb'
  6.  
  7. #print(test_reg) # \d+(?:\s*(?:mmol|mm)-?[1-4]?){1,3}\b
  8. print( [x.group() for x in re.finditer(test_reg,test_str)] )
Success #stdin #stdout 0.03s 9524KB
stdin
Standard input is empty
stdout
['3 mmol mm', '3 mmol2mm', '3 mmolmm']