0

I am trying to find a way to shrink my PHP/JavaScript code by removing all of my single and multiple line comments. Currently I have the single line removal working but I can not figure out how to make my multiple line comments remove. I have tried the following regular expressions:

\/\*[^\*\/]*\*\/
\/\*[^.\/]*\*\/

And many others. The current expression I am using to find the single lines and failing the multi lines is (\/\/[^\n]*\n|\/\*[^\*\/]*\*\/)

Expression Requirements

  • The expression must be in Perl
  • The expression must not stop at // inside the multiline comment (ie. http://url.com)
  • The expression must be able to find both single and multiline comments
  • The expression can be used to find the beginning and end of any multiline PHP/JavaScript comment.

A sample of code you can test on that I will need it for can be found here

Thank you in advance.

Mic1780
  • 1,774
  • 9
  • 23

3 Answers3

3
\/\*[\s\S]*?\*\/
  • \/\* match /*
  • [\s\S]*? match anything (non-greedy)
  • until \*\/; match */
Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • Why wouldn't I use `[.]*?` with dotall on? is \s\S better? – Mic1780 Aug 20 '13 at 19:13
  • @Mic1780 what I wrote doesn't require any flags so easier to explain. Of course you can do the _dotall_ method. With _dotall_ off you can easily combine this regexp with another that matches to the end of the line, to catch `//` comments too – Paul S. Aug 20 '13 at 20:13
0

http://www.perl.com/pub/2003/06/06/regexps.html

You're not going to have it work with a dot alone. That excludes the newline character.

You're going to need to use the /s option.

Plasmarob
  • 1,321
  • 12
  • 20
0

There is an online guide which gives the following perl regex.

(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*)

And it seems to give the correct output with your test code. But you might get into trouble with text like in line #10 where there are comments inside other elements.

Oli
  • 591
  • 4
  • 10