From b987c7207c15262aa3338ef5216bed7e71fa6f57 Mon Sep 17 00:00:00 2001 From: Levi Broderick Date: Tue, 30 Jun 2026 17:14:23 -0700 Subject: [PATCH 1/2] Fix regex usage in WPF sample --- .../CSharp/FindDialogBox.xaml.cs | 30 ++++++++++++++--- .../VisualBasic/FindDialogBox.xaml.vb | 33 +++++++++++++++---- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Wpf/DialogBoxSample/CSharp/FindDialogBox.xaml.cs b/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Wpf/DialogBoxSample/CSharp/FindDialogBox.xaml.cs index b900a19f1d..1cde227612 100644 --- a/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Wpf/DialogBoxSample/CSharp/FindDialogBox.xaml.cs +++ b/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Wpf/DialogBoxSample/CSharp/FindDialogBox.xaml.cs @@ -53,16 +53,24 @@ void findNextButton_Click(object sender, RoutedEventArgs e) // Find matches if (this.matches == null) { - string pattern = this.findWhatTextBox.Text; + RegexOptions regexOptions = RegexOptions.None; + string literalToFind = this.findWhatTextBox.Text; + + // Escape the user-typed text in case it contains regex special characters. + string pattern = Regex.Escape(literalToFind); // Match whole word? - if ((bool)this.matchWholeWordCheckBox.IsChecked) pattern = @"(?<=\W{0,1})" + pattern + @"(?=\W)"; + if ((bool)this.matchWholeWordCheckBox.IsChecked) + { + if (BeginsWithRegexWordChar(literalToFind)) pattern = @"\b" + pattern; + if (EndsWithRegexWordChar(literalToFind)) pattern = pattern + @"\b"; + } // Case sensitive - if (!(bool)this.caseSensitiveCheckBox.IsChecked) pattern = "(?i)" + pattern; + if (!(bool)this.caseSensitiveCheckBox.IsChecked) regexOptions |= RegexOptions.IgnoreCase; // Find matches - this.matches = Regex.Matches(this.textBoxToSearch.Text, pattern); + this.matches = Regex.Matches(this.textBoxToSearch.Text, pattern, regexOptions); this.matchIndex = 0; // Word not found? @@ -77,7 +85,7 @@ void findNextButton_Click(object sender, RoutedEventArgs e) // Start at beginning of matches if the last find selected the last match if (this.matchIndex == this.matches.Count) { - MessageBoxResult result = MessageBox.Show("Nmore matches found. Start at beginning?", "Find", MessageBoxButton.YesNo); + MessageBoxResult result = MessageBox.Show("No more matches found. Start at beginning?", "Find", MessageBoxButton.YesNo); if (result == MessageBoxResult.No) return; // Reset @@ -96,6 +104,18 @@ void findNextButton_Click(object sender, RoutedEventArgs e) this.matchIndex++; } + static bool BeginsWithRegexWordChar(string literal) + { + if (string.IsNullOrEmpty(literal)) return false; + return Regex.IsMatch(literal[0].ToString(), @"\w"); + } + + static bool EndsWithRegexWordChar(string literal) + { + if (string.IsNullOrEmpty(literal)) return false; + return Regex.IsMatch(literal[literal.Length - 1].ToString(), @"\w"); + } + void textBoxToSearch_TextChanged(object sender, TextChangedEventArgs e) { ResetFind(); diff --git a/dotnet-desktop-guide/samples/snippets/visualbasic/VS_Snippets_Wpf/DialogBoxSample/VisualBasic/FindDialogBox.xaml.vb b/dotnet-desktop-guide/samples/snippets/visualbasic/VS_Snippets_Wpf/DialogBoxSample/VisualBasic/FindDialogBox.xaml.vb index ec81e59a32..74f46b2b85 100644 --- a/dotnet-desktop-guide/samples/snippets/visualbasic/VS_Snippets_Wpf/DialogBoxSample/VisualBasic/FindDialogBox.xaml.vb +++ b/dotnet-desktop-guide/samples/snippets/visualbasic/VS_Snippets_Wpf/DialogBoxSample/VisualBasic/FindDialogBox.xaml.vb @@ -34,14 +34,23 @@ Namespace SDKSample Private Sub findNextButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) If (Me.matches Is Nothing) Then - Dim pattern As String = Me.findWhatTextBox.Text + Dim matchOptions As RegexOptions = RegexOptions.None + Dim literalToFind As String = Me.findWhatTextBox.Text + + ' Escape the user-typed text in case it contains regex special characters. + Dim pattern As String = Regex.Escape(literalToFind) + + ' Match whole word? If Me.matchWholeWordCheckBox.IsChecked.Value Then - pattern = ("(?<=\W{0,1})" & pattern & "(?=\W)") + If BeginsWithRegexWordChar(literalToFind) Then pattern = "\b" & pattern + If EndsWithRegexWordChar(literalToFind) Then pattern = pattern & "\b" End If - If Not Me.caseSensitiveCheckBox.IsChecked.Value Then - pattern = ("(?i)" & pattern) - End If - Me.matches = Regex.Matches(Me.textBoxToSearch.Text, pattern) + + ' Case sensitive + If Not Me.caseSensitiveCheckBox.IsChecked.Value Then matchOptions = matchOptions Or RegexOptions.IgnoreCase + + ' Find matches + Me.matches = Regex.Matches(Me.textBoxToSearch.Text, pattern, matchOptions) Me.matchIndex = 0 If (Me.matches.Count = 0) Then MessageBox.Show(("'" & Me.findWhatTextBox.Text & "' not found."), "Find") @@ -50,7 +59,7 @@ Namespace SDKSample End If End If If (Me.matchIndex = Me.matches.Count) Then - Dim result As MessageBoxResult = MessageBox.Show("Nmore matches found. Start at beginning?", "Find", MessageBoxButton.YesNo) + Dim result As MessageBoxResult = MessageBox.Show("No more matches found. Start at beginning?", "Find", MessageBoxButton.YesNo) If (result = MessageBoxResult.No) Then Return End If @@ -65,6 +74,16 @@ Namespace SDKSample Me.matchIndex += 1 End Sub + Private Shared Function BeginsWithRegexWordChar(ByVal literal As String) As Boolean + If String.IsNullOrEmpty(literal) Then Return False + Return Regex.IsMatch(literal(0).ToString(), "\w") + End Function + + Private Shared Function EndsWithRegexWordChar(ByVal literal As String) As Boolean + If String.IsNullOrEmpty(literal) Then Return False + Return Regex.IsMatch(literal(literal.Length - 1).ToString(), "\w") + End Function + Private Sub findWhatTextBox_TextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs) Me.ResetFind() End Sub From cc15ad36d17729efe7240aadd5e7054e9891df4e Mon Sep 17 00:00:00 2001 From: Levi Broderick Date: Wed, 1 Jul 2026 18:31:45 -0700 Subject: [PATCH 2/2] Fix empty string issue in regex sample --- .../DialogBoxSample/CSharp/FindDialogBox.xaml.cs | 12 ++++++++---- .../VisualBasic/FindDialogBox.xaml.vb | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Wpf/DialogBoxSample/CSharp/FindDialogBox.xaml.cs b/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Wpf/DialogBoxSample/CSharp/FindDialogBox.xaml.cs index 1cde227612..06375ce6a5 100644 --- a/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Wpf/DialogBoxSample/CSharp/FindDialogBox.xaml.cs +++ b/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Wpf/DialogBoxSample/CSharp/FindDialogBox.xaml.cs @@ -50,20 +50,24 @@ public int Length void findNextButton_Click(object sender, RoutedEventArgs e) { + string textToFind = this.findWhatTextBox.Text; + + // Short-circuit: Did the user not provide any text at all? + if (string.IsNullOrEmpty(textToFind)) return; + // Find matches if (this.matches == null) { RegexOptions regexOptions = RegexOptions.None; - string literalToFind = this.findWhatTextBox.Text; // Escape the user-typed text in case it contains regex special characters. - string pattern = Regex.Escape(literalToFind); + string pattern = Regex.Escape(textToFind); // Match whole word? if ((bool)this.matchWholeWordCheckBox.IsChecked) { - if (BeginsWithRegexWordChar(literalToFind)) pattern = @"\b" + pattern; - if (EndsWithRegexWordChar(literalToFind)) pattern = pattern + @"\b"; + if (BeginsWithRegexWordChar(textToFind)) pattern = @"\b" + pattern; + if (EndsWithRegexWordChar(textToFind)) pattern = pattern + @"\b"; } // Case sensitive diff --git a/dotnet-desktop-guide/samples/snippets/visualbasic/VS_Snippets_Wpf/DialogBoxSample/VisualBasic/FindDialogBox.xaml.vb b/dotnet-desktop-guide/samples/snippets/visualbasic/VS_Snippets_Wpf/DialogBoxSample/VisualBasic/FindDialogBox.xaml.vb index 74f46b2b85..aed591c9e0 100644 --- a/dotnet-desktop-guide/samples/snippets/visualbasic/VS_Snippets_Wpf/DialogBoxSample/VisualBasic/FindDialogBox.xaml.vb +++ b/dotnet-desktop-guide/samples/snippets/visualbasic/VS_Snippets_Wpf/DialogBoxSample/VisualBasic/FindDialogBox.xaml.vb @@ -33,17 +33,21 @@ Namespace SDKSample End Sub Private Sub findNextButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) + Dim textToFind As String = Me.findWhatTextBox.Text + + ' Short-circuit: Did the user not provide any text at all? + If String.IsNullOrEmpty(textToFind) Then Return + If (Me.matches Is Nothing) Then Dim matchOptions As RegexOptions = RegexOptions.None - Dim literalToFind As String = Me.findWhatTextBox.Text ' Escape the user-typed text in case it contains regex special characters. - Dim pattern As String = Regex.Escape(literalToFind) + Dim pattern As String = Regex.Escape(textToFind) ' Match whole word? If Me.matchWholeWordCheckBox.IsChecked.Value Then - If BeginsWithRegexWordChar(literalToFind) Then pattern = "\b" & pattern - If EndsWithRegexWordChar(literalToFind) Then pattern = pattern & "\b" + If BeginsWithRegexWordChar(textToFind) Then pattern = "\b" & pattern + If EndsWithRegexWordChar(textToFind) Then pattern = pattern & "\b" End If ' Case sensitive