|
|
Title | Use regular expressions to replace text in the lines in a string |
Description | This example shows how to use regular expressions to replace text in the lines in a string in Visual Basic 6. The program splits the text into lines, uses the RegExp class's Replace method on each line, and then joins the results. |
Keywords | regular expression, string, parsing, parse, replace, RegExp |
Categories | Strings, Algorithms |
|
|
Before using regular expressions, select the Project menu's References command and select the Microsoft VBScript Regular Expressions 1.0" library.
When the user clicks the Go button, the program makes a RegExp object. It sets the object's Pattern, IgnoreCase, and Global properties.
It then splits the text into lines, calls the object's Replace method for each, and then re-joins the lines' results. (The program must processes each line separately so the regular expression can use the ^ and $ characters to indicate the start and end of the lines. This is slightly easier in VB .NET where the Regex class understands the "(?m)" regular expression option that indicates multi-line input.)
|
|
Private Sub cmdGo_Click()
On Error Resume Next
Dim reg_exp As New RegExp
reg_exp.Pattern = txtPattern.Text
reg_exp.IgnoreCase = True
reg_exp.Global = True
' Split into lines.
Dim lines() As String
lines = Split(txtString.Text, vbCrLf)
' Make the replacements in the lines.
Dim i As Integer
For i = LBound(lines) To UBound(lines)
lines(i) = reg_exp.Replace(lines(i), _
txtReplace.Text)
Next i
' Merge the lines.
lblResult.Caption = Join(lines, vbCrLf)
End Sub
|
|
The bigger trick in this example lies in the search and replacement patterns. In this example, the search pattern is "^([^,]*), (.*)$". The pieces of this expression have the following meanings:
^ | Match the beginning of a line. |
([^,]*) | Match any character other than comma any number of times. This part is enclosed in parentheses so it forms the first match group. |
, | Match a comma followed by a space. |
(.*) | Match any character any number of times. This part is enclosed in parentheses so it forms the second match group. |
$ | Match the end of the line. |
The replacement pattern is "$2 $1". This says to replace the stuff that was matched with the second match group, a space, and then the first match group.
This example takes this text:
Archer, Ann
Baker, Bob
Carter, Cindy
Deevers, Dan
And converts it into this:
Ann Archer
Bob Baker
Cindy Carter
Dan Deevers
|
|
|
|
|
|