1

I have the following RegEx to remove commentary from a PHP file: %/\*.*?\*/%s

The side effect is, that on use of glob like glob($basedir . '/*') it starts removing everything midcode...

So how do I tell the regex to not match when there are quotes in front of it or behind it?

Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102
  • Use `preg_match` to find if there's any occupancy of "there are quotes in front of it or behind it" and cast it in boolean and use in `if-else statements`. First idea what came in my mind after reading this – Leri Jul 17 '12 at 10:27
  • 4
    This is nontrivial because you could certainly also have quotes inside comments, and then things get hairy pretty rapidly. This means that any regex-based solution will be suboptimal - in essence you need a complete PHP language parser and use that to identify comments correctly. – Tim Pietzcker Jul 17 '12 at 10:28
  • This can't be done except in the simplest cases. Most probably, **only PHP** can parse PHP (due to regexes and other non-trivial constructs). For Perl, this has [been proven some time ago](http://www.perlmonks.org/?node_id=663393). – rubber boots Jul 17 '12 at 11:25
  • Seems to match **[this question](http://stackoverflow.com/questions/503871/best-way-to-automatically-remove-comments-from-php-code)**. – rubber boots Jul 17 '12 at 11:31

1 Answers1

1

Try this :

%[^'"]{1}/\*.*?\*/[^'"]{1}%s

It include not simple or double quote before /* and after */

Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102
Zepekigno
  • 92
  • 2