Hi
I'm try to change the background of the matched by a Regex text.

Example:
input: aaa aaa aaa //double white spaces in between
output: aaa aaa aaa //change the background of all matched white spaces to red

But it's coloring the wrong text.
I have tried and searched google for some days, but with no luck.
If I can get some help i'll be very grateful .This a WPF project in VS2013.
rtbClipboardContent is a RichTextBox.
The code I tried:

    MatchCollection matches = Regex.Matches(text, RegexExpression);

    rtbClipboardContent.Document.Blocks.Add(new Paragraph(new Run(text)));

    foreach (Match m in matches)
    {
            TextPointer start = rtbClipboardContent.Document.ContentStart.GetPositionAtOffset(m.Index, LogicalDirection.Forward);
            TextPointer end = rtbClipboardContent.Document.ContentStart.GetPositionAtOffset(m.Index + m.Length, LogicalDirection.Forward);
            rtbClipboardContent.Selection.Select(start, end);
            rtbClipboardContent.Selection.ApplyPropertyValue(Run.BackgroundProperty, "red");
    }

Thanks in advance!

Recommended Answers

All 5 Replies

OK Im at the right position but i get 1 color match and my goal is to color all the matches even when i have more than 1 regex.

    MatchCollection matches = Regex.Matches(text, RegexExpression);
    rtbClipboardContent.Document.Blocks.Add(new Paragraph(new Run(text)));
    foreach (Match m in matches)
    {
            TextPointer start = rtbClipboardContent.Document.ContentStart.GetPositionAtOffset(m.Index, LogicalDirection.Forward);
            TextPointer end = rtbClipboardContent.Document.ContentStart.GetPositionAtOffset(m.Index + m.Length, LogicalDirection.Backward);
            rtbClipboardContent.Selection.Select(start, end);
            rtbClipboardContent.Selection.ApplyPropertyValue(Run.BackgroundProperty, "red");
    }

Can you please provide us with yoru Regex Match pattern?

Sorry for that. Here they are:
\s\s+
^\s+|\s+$

I need to add a loop to check the text for more then 1 pattern or to make one like so: ^\s+|\s+$|\s\s+

For the record, "\s" accounts for ONE occurance of a whitespace. NOW,you have been tacking a '+' onto the end, which means one or more. So let's break these down

\s\s+
is pretty much saying, match one whitespace, then match another collection of whitespaces. If you simply want to match say two whitespaces, I suggested something along the lines of either \s\s or \s{2}

So I want to make sure I am reading this right, you are trying to match all occurances of double whitespace (including tabs, return lines, ext).

I will say MatchCollection is made up of a collection of Match object (what a coincidence hahaha). With that being said each match has what are called Groups, which is a collection of results (there is always an index 0 of the whole match, later matches sync with items set in parenthesis in relation to the regex matter). These groups then have an Index property, that, like you guess, relate to the index in the string you are running the regex against

BTW sorry if that last response seemed rush, something came up so I had to hurry it up. I"ll try and see if I can come back to this tonight

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.