Skip to content

A Swiss Army knife for developers.

Fix #242 - RegEx matches in multiline text don't highlight property (#381)

Etienne Baudoux0483499a3b87
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 de5d12bbb..1acad443b 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 @@ namespace DevToys.ViewModels.Tools.RegEx
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 @@ namespace DevToys.ViewModels.Tools.RegEx
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;
+ }
}
}