January 22, 2010 at 6:13 pm
can anyone point out my logic hole here? I've googled around, and cannot seem to get any example to work so far.
I'm trying to capture whatever is between two words; my example here is whatever is between "select" and "from", which might be an asterisk, or a string of column names, etc.
i think my pattern should be this:
"\bselect\b(.*?)\bfrom\b"
as i understand it \b will designate a word boundary, so i don't get words that happen to contain "from", like "fromdate" or anything like that. i've tried a number of different patterns so far,which you can review below.
the stuff in parenthesis should return one or more characters between the two words,right?
the statement could have multiple CrLf in it, and that might be why I'm not matching, but I'm not seeing it yet...i thought that the option System.Text.RegularExpressions.RegexOptions.Multiline allowed me to search multi line strings.
in a given statement, there might be more than one group of select...from, so i need to loop thru each match.
throw this code in a button event of a windows application to test:
Dim rm As System.Text.RegularExpressions.MatchCollection
'Dim m As System.Text.RegularExpressions.Match
Dim pattern As String
Dim sql As String
sql = "select * " & vbCrLf
sql = sql & "from syscolumns " & vbCrLf
sql = sql & "where id in(select id " & vbCrLf
sql = sql & " from sysobjects " & vbCrLf
sql = sql & " where xtype='U') " & vbCrLf
pattern = "\(select\).*?\(from\)"
pattern = "\b(select)\b(.*?)\b(from)\b"
pattern = ".*select (.+) from.*"
pattern = "\bselect\b(.*?)\bfrom\b"
' pattern = "\(select\).*?\(from\)"
rm = System.Text.RegularExpressions.Regex.Matches(sql, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase Or System.Text.RegularExpressions.RegexOptions.Multiline)
For Each mt As System.Text.RegularExpressions.Match In rm
Debug.Print(mt.Value)
Next
Lowell
January 23, 2010 at 12:43 pm
discovered that my problem was the period in the pattern...the period matches an character except a new line...the patter i'm using, if anyone else ever needs a regular expression to capture between two words,including those two words, is this:
pattern = "\bselect\b([\s\S]*?)\bfrom\b"
Lowell
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply