RegEx
- From: "HSalim[MVP]" <HSalim@xxxxxxx>
- Date: Wed, 8 Mar 2006 17:51:45 -0500
Hi,
I want to use RegEx to parse a text string (I want to extract the tables
referenced in a SQL statement.)
The definition of the search pattern can be described as
find "FROM" or "JOIN" AND 'followed by
(word.word.word)
OR (word..word) 'yes, that's 2 dots
OR (word.word)
OR (..word)
OR (.word)
OR (word)
and to capture that pattern into a variable.
Example:
strText = "select * from TableName1 inner join asdf.TableName2 on something
and thing join Table3 on "
i need to extract
TableName1
asdf.TableName2
Table3
I have tried to experiment with this but can't seem to get it right.
What am I doing wrong? see code below
Thanks in advance for your assistance
Regards
HS
------------------------------------------------------------------
Sub ParseSQL()
Dim regex, Match, Matches, Patrn, strText ' Create variable.
'Patrn = "(\bFrom|Join\b)([\w\.]*){1,3}\b"
'Patrn = "(\bFrom|Join\b)(\w*\.){0,2}(\w*)\b"
Patrn = "(([A-Z][A-Z0-9_]*)\.{0,2})(\w*)\b"
strText = "select * from TableName1 inner join asdf.TableName2 on
something and thing join Table3 on "
' strText = "select * from dbo.TableName1 inner join .TableName2 on
something join ABCDB..Table3 on "
Set regex = New RegExp ' Create a regular expression.
regex.Pattern = Patrn ' Set pattern.
regex.IgnoreCase = True ' Set case insensitivity.
regex.Global = True ' Set global applicability.
Set Matches = regex.Execute(strText) ' Execute search.
Debug.Print "Num matches: " & Matches.Count
For Each Match In Matches ' Iterate Matches collection.
retstr = " Pos: " & Match.FirstIndex & "; Val: " & Match.Value & ";"
If Match.SubMatches.Count > 0 Then
For i = 0 To Match.SubMatches.Count - 1
retstr = retstr & "Sub(" & i & ")= " & Match.SubMatches(i) & ";"
Next
End If
Debug.Print retstr
'Debug.Print Match.SubMatches(1)
Next
End Sub
.
- Follow-Ups:
- Re: RegEx
- From: Steve Fulton
- Re: RegEx
- Prev by Date: xpath vbscript
- Next by Date: Starting Office Application Files
- Previous by thread: xpath vbscript
- Next by thread: Re: RegEx
- Index(es):
Relevant Pages
|