regex for tokenizing simple SQL SELECT string statements?

ridhwaans

Active member
Joined
Jun 1, 2012
Messages
34
Programming Experience
3-5
currently I'm using vb's string functions (such as .replace, .split, .mid) to parse and process the columns and conditions of a sql SELECT statement into two listboxes
It works fine, BUT, I'm trying to find a more efficient way to perform this using the regex alternative. I'm not very good with regex, thats why Im asking

the regex shouldnt be too hard because the SQL statements ONLY have basic SELECT and WHERE clauses for only ONE table/view, and no JOINs, no OR
For example,

VB.NET:
SELECT ID, PurchaserFirstName, PurchaseDate, Email, PhoneNumber FROM Accounts WHERE PurchaserFirstName <> "John"  AND PurchaseDate >= "05/02/2004" AND Email LIKE "%yahoo%"

the sql statement above is as hard as it would get
So, with my vb code, the statement would translate into two listboxes

listbox1 items
ID
PurchaserFirstName
PurchaserDate
Email
PhoneNumber

listbox2 items
PurchaserFirstName <> "John"
PurchaseDate >= "05/02/2004"
Email LIKE "%yahoo%"
 
Last edited:
Regex is always hard! It gives me a headache just thinking about it! It may be possible to streamline your string parsing but I'd stick with it! Or is that just me?
 
Can you clarify for me what you want your regex to do? Validate your SQL string to be sure its not bogus input? I use regex for validation but cant say ive used it for parsing....
 
Can you clarify for me what you want your regex to do? Validate your SQL string to be sure its not bogus input? I use regex for validation but cant say ive used it for parsing....

Lotok, regex will parse the string. The SQL string input will never be bogus; validation is not necessary
I want regex to split the SQL string of its SELECT fields and WHERE conditions.
Relevant vb split snippet:

VB.NET:
Module Module1      Sub Main() 	[I]' We want to split this input string[/I] 	Dim s As String = "there is a cat"  	[I]' Split string based on spaces[/I] 	Dim words As String() = s.[U]Split[/U](New Char() {" "c})  	[I]' Use For Each loop over words and display them[/I] 	Dim word As String 	For Each word In words 	    Console.WriteLine(word) 	Next     End Sub  End Module

So, in the case of the SQL string, keywords like "SELECT ", " AND ", " WHERE " and commas would act as delimiters
 
Back
Top