【发布时间】:2018-01-12 23:49:45
【问题描述】:
我需要使用 RegEx 解析以下字符串。
abc = 'def' and size = '1 x(3\" x 5\")' and (name='Sam O\'neal')
这是一个 SQL 过滤器,我想使用以下分隔符将其拆分为标记:
(, ), >,<,=, whitespace, <=, >=, !=
解析字符串后,我希望输出为:
abc,
=,
def,
and,
size,
=,
'1 up(3\" x 5\")',
and,
(,
Sam O\'neal,
),
我尝试了以下代码:
string pattern = @"(<=|>=|!=|=|>|<|\)|\(|\s+)";
var tokens = new List<string>(Regex.Split(filter, pattern));
tokens.RemoveAll(x => String.IsNullOrWhiteSpace(x));
我不确定如何将单引号中的字符串作为一个标记。我是 Regex 的新手,希望能提供任何帮助。
【问题讨论】:
-
在
(之后添加'[^']*'|。 -
谢谢@WiktorStribiżew - 我用以下模式尝试了你的建议:'string pattern = @"(=|!=|=|>|