FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Help with regular expression





erlendhg
Hi.
Does anyone of you know PCRE regular expressions?
I need help with an expression used in PHP's preg_replace function.

What I try to do is to match C/C++ comment lines that starts with // and make the text green.
Also, if the line ends with a backslash (\), I want the comment to continue on the next line.

So far, this is what I have written:

Code:
$content = preg_replace_callback('/\/\/.*?(?<!\\\\)[\n]/s', create_function('$matches', 'return "<font color=\"#008000\">" . strip_tags($matches[0]) . "</font>";'), $content);


If $content contains:
Code:
Random code // Comment \
Here too


I get:
Random code // Comment \
Here too


So this works fine, but if there is one or more whitespaces behind the backslash in line one, only the comment on the first line gets green (and that I don't want). I would like both comment lines to be green anyways, and I have tried and failed with different patterns several times now, so I hope one of you please can help me.

Thanks


--- EDIT ----

Since no one answered in the space of time I had to finish this, I just had to find it out myself.
After even more trial and error, I actually solved it. The solution was to remove the back assertion and use a more "general" pattern:
Code:
'/\/\/(.*\\\\[\s]*)*.*$/m'
coreymanshack
Yea it's usually how I solve my programming questions, I opened your post, looked at it.. and said... gawd.. I DO NOT want to mess with regular expressions like this. Sorry Very Happy
Related topics
PHP Regex
[JS] Regular expressions global match get first group
Google analytics help
Highlighting Search Terms
regular expression
Textbox Submit to email
Using PHP toreciever information from external source.
Complicated regular expression (for preg_replace)
Formulate Regular Expression
Help for REgular Expression!
Extraer los canales de color en una imagen a color
How to do this find and replace
.htaccess code help needed
Python Expression Evaluator Version 2
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.