Leandro Bardelli
Leandro Bardelli

Reputation: 11578

Highlight words with RegEx on RichTextBox with SilverLight

Im trying to highlight words, letters or anything marked in a Regular Expression. Im using RichTextBox in Silverlight (take care that Silverlight has NOT uses the same .NET Framework than others apps, for example it HAS NOT the TextRange)

Everytime the text has a \r\n or \n\r the pointers moves 4 position forward. But the really bad is when i do the highlight, the style text add 4 positions more. Even, i think he has more erratic behavior than racional. Please advice me something in this!

Thanks FailDev.

The text im using for try this is:

ffffffffffafffffffffff fffffafffffffffff fffffffffffffafffffffff

(4 lines, after the last f there is a carrier return \ line feed)

Also this one doesnt work with my code (has the erratic behavior, or not):

ffffffffffaaffffffafffff fffffafffffffffff fffffffffffffafffffffff

(4 lines, after the last f there is a carrier return \line feed)

The idea is highlight with blue, bold and underline the "a" character.

The last code that i tried is this:

MatchCollection CarrierReturns =  MainGeneral.Build_Regex(@"[\r\n|\n\r]", false);

            MatchCollection WordsFound =  MainGeneral.Build_Regex("[a]", false);
            foreach (Match x in WordsFound) {
                int StringCarrierCount = 0;

                if (CarrierReturns.Count > 0)
                {
                    foreach (Match CR in CarrierReturns)
                    {
                        if (x.Index > CR.Index)
                        {
                            textBox1.Text += " CI:" + StringCarrierCount + "- ";
                            break;
                        }
                        StringCarrierCount++;
                    }
                }

                if (x.Index < CarrierReturns[0].Index) { StringCarrierCount = 0; }

                StringBegin = 2 + x.Index + (4 * StringColorCounter) + (4 *  StringCarrierCount);
                StringEnd = StringBegin + x.Length;
                textBox1.Text += StringBegin + "-" + StringEnd + " ";
                StringColorCounter++;

                StartPoint = MainData.ContentStart.GetPositionAtOffset(StringBegin, LogicalDirection.Forward);
                EndPoint = MainData.ContentStart.GetPositionAtOffset(StringEnd, LogicalDirection.Forward);
                MainData.Selection.Select(StartPoint, EndPoint);
                MainData.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
                MainData.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
                MainData.Selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
            }

Thanks in advance, please ask me before rate bad! :)

Upvotes: 3

Views: 907

Answers (1)

Alan Moore
Alan Moore

Reputation: 75222

A line separator may be any of \r\n (carriage-return + linefeed), \n (linefeed alone), or \r (carriage-return alone). The correct way to match one would be with \r\n|[\r\n].

Your regex - [\r\n|\n\r] - matches exactly one character; any of \r, \n, or |. That means, whenever the line separator is \r\n, your code acts like there are two line separators, not one.

There may be other errors in your code as well, but I don't really understand what it's trying to do. I'm sure the regex is incorrect.

Upvotes: 1

Related Questions