fork download
  1. import re
  2. p = re.compile(r'(\b\d+(?:\.\d+)?(?:\spercent|\%))')
  3. test_str = "The grades of the two students improved by 5.2% and 5.4% it was 5 percent or 1.2%."
  4. result = re.sub(p, "__PERCENTAGE__", test_str)
  5. print (result)
Success #stdin #stdout 0.02s 8736KB
stdin
Standard input is empty
stdout
The grades of the two students improved by __PERCENTAGE__ and __PERCENTAGE__ it was __PERCENTAGE__ or __PERCENTAGE__.