fork download
  1. import re
  2. p = "The pen is on the table."
  3. def repl(x):
  4. if x.group(1):
  5. return "B{}".format("I"*len(x.group(2)))
  6. elif x.group(3):
  7. return "B"
  8. else:
  9. return "S"
  10.  
  11. print( re.sub(r'(\w)(\w*)|([^\w\s])|\s', repl, p) )
  12. # => BIISBIISBISBISBIISBIIIIB
  13.  
Success #stdin #stdout 0.02s 9556KB
stdin
Standard input is empty
stdout
BIISBIISBISBISBIISBIIIIB