fork download
  1. import re
  2.  
  3. regex = r"(?<!\d)((\()?\d+(?:[.-]\d+)?(?(2)\)))\*\1(?!\d)"
  4.  
  5. test_str = ("5*5 --> 5 \n"
  6. "3.14*4 --> no return \n"
  7. "(10-4)*(10-4) --> (10-4)")
  8.  
  9. for m in re.finditer(regex, test_str):
  10. print(m.group(1))
Success #stdin #stdout 0.02s 9332KB
stdin
Standard input is empty
stdout
5
(10-4)