Regex? C# - Highlight words inside a RichTextBox is not working because of "/r/n"

I used an idea of a guy outsider ee to higlight keywords. It follows the code:

public Regex keyWords = (new Regex"abstract|as|base|bool....|....|....");

int selPos = richTextBox1.SelectionStart;  

            foreach (Match keyWordMatch in keyWords.Matches(richTextBox1.Text))  
             richTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length);  
             richTextBox1.SelectionColor = Color.Blue;  
              richTextBox1.SelectionStart = selPos;  
            richTextBox1.SelectionColor = Color.Black;  

the keywords are not correctly highlighted. I think that is  because  Index is not counting /r/n. I don't want to replace them because richtextbox will be unformatted?

Any easy solution ?
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.

Terry WoodsIT GuruCommented:
Unless you want to pick up partial words like the base in database, you'll want to change your pattern to:

It sounds like it may not be your only problem though. Can you provide a screenshot of the output?
novreisbAuthor Commented:
Thanks for the suggestion, but sometimes keywords come connected xxx/yyy/ddd or xxx-yyy-ddd, and so on! It is a risk I assume!

Definitively, the problem is with "/r/n", because when I replace  them by  "", the words were correctly highlighted! My problem? Text got unformatted and I don't want that!
Terry WoodsIT GuruCommented:
\b matches a word boundary, which exists between any character matching [a-zA-Z0-9_] and a character not matching that. If I understand correctly, the examples you gave would work nicely with a word boundary.

My specialty is regular expressions, not C#, so hopefully someone else can help you with the \r\n problem.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

novreisbAuthor Commented:
thks Terry
novreisbAuthor Commented:
Ok, I got it! For a beginner like me this can help!

foreach (Match keyWordMatch in keyWords.Matches(body))
                textBox6.Select(keyWordMatch.Index, keyWordMatch.Length);
                string haystack = body.Substring(0,keyWordMatch.Index);
                string needle = "\r\n";
                int needleCount = (haystack.Length - haystack.Replace(needle, "").Length) / needle.Length;
                textBox6.Select(keyWordMatch.Index - needleCount, keyWordMatch.Length);
                textBox6.SelectionColor = Color.Red;
                textBox6.SelectionStart = selPos;
                textBox6.SelectionColor = Color.Black;

solution was found outsider EE so thanks for those posters

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
Terry WoodsIT GuruCommented:
Glad you got it solved, and thanks for sharing your solution!
novreisbAuthor Commented:
Once I saw that regex was not helping me, I mixed 2 solutions after binging the questions (how to highlight text on a richtextbob and how to  count occurrences of a string inside a string). That's it!
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
Microsoft Development

From novice to tech pro — start learning today.