-1

I am trying to write a Regex that will select everything between a whitespace and =.

From the following lines

Window x:Class="QuiddlerGUI.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

I would like it to select x:Class and xmlns. The closest I could get was this but it was not stopping at white space.

(?<=)(.*?)(?==)       

I am using the regex to try and select text in a RichTextBox to attempt and change the color of the text.

foreach(TextColors color in textColors)
    {
        var start = body.Document.ContentStart;

        while (start != null && start.CompareTo(body.Document.ContentEnd) < 0)
        {
            if (start.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
            {
                var match = color.RegularExpression.Match(start.GetTextInRun(LogicalDirection.Forward));

                var textrange = new TextRange(start.GetPositionAtOffset(match.Index, LogicalDirection.Forward), start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward));

                textrange.ApplyPropertyValue(TextElement.ForegroundProperty, color.TextColor);
                    start = textrange.End;

                }
                start = start.GetNextContextPosition(LogicalDirection.Forward);
            }
        }
jsomers89
  • 173
  • 1
  • 11

2 Answers2

1

Try with (?<=\s)[^=]*

Explanation:

  • (?<=\s) will look behind for a whitespace.

  • [^=]* will match everything until a = is met.

Regex101 Demo

1

Have you tried something like:

" (.*?)="

It search for a space, followed by any character until it founds an equal sign, looking for the shortest string (regexp are greedy).