Page 567
LISTING 19.8: THE REPLACE ALL BUTTON'S CODE |
Private Sub bttnReplace_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles bttnReplace.Click If chkRegEx.Checked Then Dim searchOptions As RegexOptions searchOptions = RegexOptions.Multiline If Not chkCase.Checked Then searchOptions = searchOptions Or RegexOptions.IgnoreCase End If RegEx = New System.Text.RegularExpressions.Regex( _ searchWord.Text, searchOptions) Dim selStart As Integer = EditorForm.txtBox.SelectionStart Dim replacementText As String replacementText = RegEx.Replace( _ EditorForm.txtBox.SelectedText, _ replaceWord.Text, _ System.Text.RegularExpressions._ RegexOptions.Multiline) EditorForm.txtBox.SelectedText = replacementText EditorForm.txtBox.Select(selStart, replacementText.Length) EditorForm.txtBox.ScrollToCaret() Else If EditorForm.txtBox.SelectedText <> '' " Then EditorForm.txtBox.SelectedText = replaceWord.Text End If End If bttnFindNext.PerformClick() End Sub |
You can experiment with the RegExEditor project, or even use it as a starting point for a highly specialized editor. Let's move on to some more advanced topics in regular expressions.
Advanced Topics in Regular Expressions
So far you've learned the basics of regular expressions. The metacharacters and symbols you've seen so far are adequate for many practical applications, but there are more topics to explore in regular expressions.
First, we'll examine the grouping of matches in a regular expression. A lengthy regular expression can be broken into simpler ones, which you can refer to later in the same regular expression. Being able to refer to previous matches allows you to perform very powerful searches (such as locating repeated words in a text).