fork download
  1. import re
  2. s = "mandatory NORMAL\r\nP1000 NONE\r\nof NONE\r\ncoastal NONE\r\n35 NONE \r\nwas NONE\r\nordered \r\n. NONE\r\nWith NORMAL\r\nthe NONE\r\ntyphoon \r\nout NONE\r\nof NONE\r\nthe NONE\r\ncountry NORMAL\r\nlocal NORMAL\r\ngovernments \r\n1000,000 NORMAL"
  3. words = []
  4. def repl(m):
  5. words.append(m.group(1))
  6. if m.group(2):
  7. return "{} NUMBER".format(m.group(1))
  8. return m.group(1)
  9.  
  10. res = re.sub(r"([^\s0-9]*[0-9]\S*)(\s+\w+)?", repl, s)
  11. print("Result: {}".format(res))
  12. print("Words: {}".format(", ".join(words)))
Success #stdin #stdout 0s 23304KB
stdin
Standard input is empty
stdout
Result: mandatory NORMAL
P1000 NUMBER
of NONE
coastal NONE
35 NUMBER 
was NONE
ordered 
. NONE
With NORMAL
the NONE
typhoon 
out NONE
of NONE
the NONE
country NORMAL
local NORMAL
governments 
1000,000 NUMBER
Words: P1000, 35, 1000,000