我正在尝试编写一个简单的 sql 解析器来将 sql 语句分解为其基本部分。但是,我遇到了嵌套查询的问题。一个例子最好地说明:
sql = "select * from Customers where id in (select customer_id from Orders where 1=1)"
Set re = New RegExp
re.IgnoreCase = True
re.Pattern = "^(.*)\swhere\s(.*)$"
re.Global = True
Set matches = re.Execute( sql )
If matches.count > 0 Then
Set submatches = matches(0).Submatches
where_part = Trim(submatches(1))
everything_else = Trim(submatches(0))
End If
Response.Write where_part & "<br>"
Response.Write everything_else & "<br>"
在这里,我希望 where_part 包含“id in (select customer_id from Orders where 1=1)”,而 Everything_else 包含“select * from Customers”。也就是说,我希望它对第二个(。*)尽可能贪婪,对第一个无私。或者换一种说法,我希望everything_else 不包含任何地方。
但是,我得到了 everything_else = "select * from Customers where id in (select customer_id from Orders" and where_part = "1=1)"
如何在不使用混乱的非正则表达式字符串解析的情况下解决这个问题?