Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to strip comments and multi-line comments and empty lines

I want to parse a file and I want to use php and regex to strip:

  • blank or empty lines
  • single line comments
  • multi line comments

basically I want to remove any line containing

/* text */ 

or multi line comments

/***
some
text
*****/

If possible, another regex to check if the line is empty (Remove blank lines)

Is that possible? can somebody post to me a regex that does just that?

Thanks a lot.

like image 302
Ahmad Fouad Avatar asked Mar 13 '09 14:03

Ahmad Fouad


4 Answers

$text = preg_replace('!/\*.*?\*/!s', '', $text);
$text = preg_replace('/\n\s*\n/', "\n", $text);
like image 86
chaos Avatar answered Oct 27 '22 21:10

chaos


Keep in mind that any regex you use will fail if the file you're parsing has a string containing something that matches these conditions. For example, it would turn this:

print "/* a comment */";

Into this:

print "";

Which is probably not what you want. But maybe it is, I don't know. Anyway, regexes technically can't parse data in a manner to avoid that problem. I say technically because modern PCRE regexes have tacked on a number of hacks to make them both capable of doing this and, more importantly, no longer regular expressions, but whatever. If you want to avoid stripping these things inside quotes or in other situations, there is no substitute for a full-blown parser (albeit it can still be pretty simple).

like image 42
Chris Lutz Avatar answered Oct 27 '22 23:10

Chris Lutz


//  Removes multi-line comments and does not create
//  a blank line, also treats white spaces/tabs 
$text = preg_replace('!^[ \t]*/\*.*?\*/[ \t]*[\r\n]!s', '', $text);

//  Removes single line '//' comments, treats blank characters
$text = preg_replace('![ \t]*//.*[ \t]*[\r\n]!', '', $text);

//  Strip blank lines
$text = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $text);
like image 44
makaveli_lcf Avatar answered Oct 27 '22 21:10

makaveli_lcf


$string = preg_replace('#/\*[^*]*\*+([^/][^*]*\*+)*/#', '', $string);
like image 23
Federico Biccheddu Avatar answered Oct 27 '22 21:10

Federico Biccheddu