1. $/ = undef;
  2. $string = <>;
  3.  
  4. $string =~ s{--\[\[([^\]]|\][^\]])*--[^\r\n]*\]\]|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^-"'\\]*)}{defined $2 ? $2 : ""}gse;
  5. $string =~ s{--[^\n\r]*|(\[\[([^\]]|\][^\]])*\]\]|"(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.([^-\["'\\]|\[[^-\["'\\])*)}{defined $1 ? $1 : ""}gse;
  6. $string =~ s{\s+|(\[\[([^\]]|\][^\]])*\]\]|"(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.([^-\s\["'\\]|\[[^-\s\["'\\])*)}{defined $1 ? $1 : " "}gse;
  7. $string =~ s{
  8. \ ?([-+*\/%^={}().,;[\]])\ ? ## this matches every character lua doesn't care about wether a space is in front/behind of it. Example: "a = 5" equals "a=5".
  9. | ## the part that makes quotes immume to the above match
  10. ( ## open the second catch
  11. \[\[ ## an escape string opens
  12. (
  13. [^\]]|\][^\]] ## match until closure
  14. )*
  15. \]\] ## close
  16. |
  17. " ## an quote opens
  18. (
  19. \\. ## escaped char (to prevent closure on \")
  20. |
  21. [^"\\] ## not " or \
  22. )*
  23. " ## close
  24. |
  25. ' ## an quote opens
  26. (
  27. \\. ## escaped char (to prevent closure on \")
  28. |
  29. [^'\\] ## not ' or \
  30. )*
  31. ' ## close
  32. |
  33. . ## any char
  34. ([^-\ +*\/%^={}().,;[\]\["'\\]|\[[^-\ +*\/%^={}(),;[\]\["'\\])* ## none of the things that we want to match for
  35. )
  36. }{
  37. defined $2 ? $2 : "$1"
  38. }gxse;
  39.  
  40. print $string;