C++ syntax highlight with Regular Expressions

I want to colorize a C++ code by highlighting keywords in blue before outputting it inside HTML page. The problem is that if two keywords are one just after another, the followed word is not colorized (ex: in "const int i = 0;" just "const" will be in blue). So what I must do to show to the preg_replace to return to one character back after a match ?

The code I use is :

<?php
[...]
$delimiters = "\\r|\\n|\\t|\\x20|\\x21|\\x25|\\x40|\\x7E|\\x21|\\x22|\\x25|\\x26|\\x27|\\x28|\\x29|\\x2A|\\x2B|\\x2C|\\x2D|\\x2E|\\x2F|\\x3A|\\x3B|\\x3C|\\x3D|\\x3E|\\x3F|\\x40|\\x5B|\\x5C|\\x5D|\\x5E|\\x7B|\\x7C|\\x7D|\\x7E";
$from = "\"(" . $delimiters . ")(auto|int|bool|short|long|void|...|#ifndef|#undef)(" . $delimiters . ")\"";
$to = "$1<font color=blue>$2</font>$3";
$new_code = preg_replace($from, $to, $new_code);
[...]
?>
MainMaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
Isn't it easier to tokenize the string and then highlight words from your functionlist accordingly?

-r-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MainMaAuthor Commented:
Sorry, what do you mean exactly by tokenize the string ?
0
RoonaanCommented:
Tokenizing is splitting a large body of text into tokens,ie:
a = 1 + 5 + pow(1,2)
tokenized:
IDENTIFIER, EQUALS, NUM, ADD, IDENTIFIER, ANCHOR_LEFT, NUM, COMMA, NUM, ANCHOR_RIGHT.

Then you can give all NUM tokens a certain color, decide to filter all IDENTIFIER tokens with your function list and give the function one color, and other items another color.

With some work you could have this parser colorize the whole code in a single pass.

-r-
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

MainMaAuthor Commented:
Ok. I see.
It seems to me quite difficult to realize the task this way, but I will try to do it nextly...

Also, I've just tried to colorize the script my way, but by adding "??" at the end:

<?php
[...]
$from = "\"(" . $delimiters . ")(auto|int|bool|short|long|void|...|#ifndef|#undef)(" . $delimiters . ")??\"";
[...]
?>

Seems that it works, but will it slow down the process ? I will use the function on a large amounts of code, so the speed is quite important...
0
RoonaanCommented:
You should be fine with a single questionmark I think.

-r-
0
MainMaAuthor Commented:
You should be fine with a single questionmark I think.>

No, it doesn't work. With a single questionmark, the effect is the same as without questionmark at all... :(
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.