fork download
  1. import re
  2.  
  3. code = r"""
  4. print(", ,", "l") # comment spaces
  5. print(", ,", "l") # comment spaces
  6. """
  7.  
  8. print(re.sub(r"""
  9. [Rr]("(?!"")|'(?!'')|"{3}|'{3}).*?\1 # Raw strings
  10. | ("(?!"")|'(?!'')|"{3}|'{3})(?:\\.|[^\\])*?\2 # Normal strings
  11. | [ \t]*\#[^\r\n]* # Comments
  12. | ^[ \t]+ # Leading whitespace
  13. | [ \t]+$ # Trailing whitespace
  14. | ([ \t]{2,}) # Duplicate whitespace
  15. """,
  16. lambda m: m.group(0) if m.group(3) is None else ' ',
  17. code,
  18. flags = re.VERBOSE | re.DOTALL | re.MULTILINE))
  19.  
Success #stdin #stdout 0.11s 10096KB
stdin
Standard input is empty
stdout
    print(",  ,", "l")   # comment  spaces
    print(",  ,", "l")   # comment  spaces