المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : regular expressions



C# Programming
04-09-2009, 03:51 PM
I'm creating a code editor with syntax highlighting and need help with the regular expressions. I've read a couple pages about it but can't figure out the exact way to do what I want.

For now, I only need support for comments and keywords like 'if', 'else', and so on. For the comments I created the following pattern:
string commentPattern = @"%.*";

It means that it matches everything to the right of, and including, the '%' character. This should be colored green. This works fine. The next pattern is for the keywords and looks like this:
string syntaxPattern = @"(\bif\b)|(\bwhile\b)|(\bclassdef\b)|(\bproperties\b)|(\bend\b)|(\bmethods\b)|(\bfunction\b)|(\belse\ b)|(\bfor\b)";

It means it matches any of these words. These keywords are colored blue. It works fine too. BUT, when I run these two patterns parallell the keywords are colored blue even if they are in a comment. How can I create a pattern that doesn't color the keywords blue if there is a '%' character to the left of them???

Thanks for help!