我有我需要使用RegEx解析以下字符串。C#正則表達式提取字符串括在單引號
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));
我不確定如何將單引號中的字符串保留爲一個令牌。我是新來的正規表達,並會感謝任何幫助。
後'(''添加 '[^'] *'|'。 –
Thanks @WiktorStribiżew - 我嘗試了以下模式的建議:'string pattern = @「(<=|> = |!= | = |> | <| \)| \(|'[^'] *'| \ s +) 「;」。雖然字符串文字'1 up(3 \「x 5」)'顯示爲一個標記。但字面上的'Sam O \'neal'顯示爲兩個標記 –