When parsing a text file that contains quoted strings - such as a VB source file - you might want to quickly locate and extract all the quoted strings. Thanks to regular expressions and the RegExp object in the Microsoft VBScript Regular Expression type library, this task is surprisingly easy: |
Click here to copy the following block | Dim re As New RegExp Dim ma As Match
re.Pattern = """.*"""
re.Global = True
For Each ma In re.Execute(txtSource.Text) Print ma.Value & " at index " & ma.FirstIndex Next |
The following routine has only one drawback: it doesn't account for repeated double quote chars, which should translate to a single double quote. See how you can remedy to this problem: |
Click here to copy the following block | Dim re As New RegExp Dim ma As Match Dim tmpText As String Dim maText As String
re.Pattern = """.*"""
re.Global = True
tmpText = Replace(txtSource.Text, """""", vbNullChar & vbNullChar)
For Each ma In re.Execute(tmpText) tmpText = Replace(ma.Value, vbNullChar & vbNullChar, """""") Print tmpText & " at index " & ma.FirstIndex Next |
|