fork download
  1. import re
  2. rx = r"(?<!\d)(\d+)\*\1(?!\d)|(\([\d+-]+\))\*\2"
  3. texts = ["5*5", "3.14*4", "(10-4)*(10-4)"]
  4. for text in texts:
  5. print( [f'{x.group(1) or ""}{x.group(2) or ""}' for x in re.finditer(r'(?<!\d)(\d+)\*\1(?!\d)|(\([\d+-]+\))\*\2', text)] )
Success #stdin #stdout 0.02s 9532KB
stdin
Standard input is empty
stdout
['5']
[]
['(10-4)']