diff --git a/src/dev/impl/DevToys/ViewModels/Tools/Text/RegEx/RegExToolViewModel.cs b/src/dev/impl/DevToys/ViewModels/Tools/Text/RegEx/RegExToolViewModel.cs index de5d12bbbe..1acad443bb 100644 --- a/src/dev/impl/DevToys/ViewModels/Tools/Text/RegEx/RegExToolViewModel.cs +++ b/src/dev/impl/DevToys/ViewModels/Tools/Text/RegEx/RegExToolViewModel.cs @@ -241,14 +241,15 @@ await ThreadHelper.RunOnUIThreadAsync(() => string? pattern = data.pattern.Trim('/'); var regex = new Regex(data.pattern, GetOptions()); - MatchCollection matches = regex.Matches(data.text.Replace("\r\n", "\r")); + MatchCollection matches = regex.Matches(data.text); foreach (Match match in matches) { + int lineCount = CountLines(data.text, match.Index); spans.Add( new HighlightSpan() { - StartIndex = match.Index, + StartIndex = match.Index - lineCount, Length = match.Length, BackgroundColor = highlighterBackgroundColor, ForegroundColor = highlighterForegroundColor @@ -311,5 +312,22 @@ private RegexOptions GetOptions() return options; } + + private int CountLines(string input, int maxLength) + { + int lines = 0; + int i = 0; + while (i > -1 && i < maxLength) + { + i = input.IndexOf("\r\n", startIndex: i); + if (i > -1 && i < maxLength) + { + lines++; + i++; + } + } + + return lines; + } } }