Jump to content

Attempting to match code style comments


Prismatic

Recommended Posts

Here's what I want to do, search through posted code and find the 3 most common types of comments,

 

/* */

//

and #

 

// and # are single line so I thought something like this would work

 

/** match //text */
$code = preg_replace("/\/\/(.*?)(.*?)/", " [color=green][b]//\\1[/b][/color] ", $code);

/** match #text */
$code = preg_replace("/\#(.*?)/", " [color=lightgreen][b]#\\1[/b][/color] ", $code);

 

However it's only grabbing the # and // leaving everything to the right out

 

And /* comments can run until they hit a */, which makes the multi line, I tried this but it also doesn't work

 

$code = preg_replace("/\/\*(.*?)\/\*/s", " [color=green][b]/*\\1*/[/b][/color] ", $code); 

 

anyone know what I need to do to make these work?

Link to comment
https://forums.phpfreaks.com/topic/130612-attempting-to-match-code-style-comments/
Share on other sites

You might want to consider a pre-made syntax highlighter for PHP (many are available), the issue with any regex pattern for this operation would be how do you keep from matching your target strings within string declarations etc.

 

/\*(??!\*/).)*\*/|(?://|#)[^\r]*

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.