2

考虑以下查询:

select
    foo,
    bar,
    yourmom
from
    theTable

select top 10 *
from
    theTable

select distinct foo, bar + 1, yourmom from theTable

我想要一个可以提取的正则表达式查询:

foo,
bar,
yourmom

*

foo, bar + 1, yourmom

分别。

我试过^\sselect\s(distinct\s)?(top\s\d*)?(?'columns'.*\s)from[\s.]*$了,我认为这会奏效,但没有。我已经玩了一段时间了,但我仍然无法让它在所有三个测试用例中工作。有人可以帮我解决他们的正则表达式吗?

4

2 回答 2

3

编辑:首先,您需要使 .-match 每个字符,包括换行符。在 java 中您可以设置 DOTALL 标志,但在 C# 中我相信您应该使用该RegexOptions.SingleLine选项。

那么这个表达式应该可以工作:

^\s*select\s+(?:distinct\s+)?(?:top\s+\d*\s+)?(?'columns'.*?)from.*$
于 2012-04-18T15:48:20.733 回答
1

我认为为 SQL 查询编写一个“适当的”解析器实际上比使用正则表达式更容易(检查Irony:它很棒并且带有一个 SQL 示例)。

于 2012-04-18T15:53:56.780 回答