C# regex extract string enclosed into single quotes

Viewed 311

I've the following string that I need to parse using RegEx.

abc = 'def' and size = '1 x(3\" x 5\")' and (name='Sam O\'neal')

This is an SQL filter, which I'd like to split into tokens using the following separators:

(, ), >,<,=, whitespace, <=, >=, !=

After the string is parsed, I'd like the output to be:

abc,
=,
def,
and,
size,
=,
'1 up(3\" x 5\")',
and,
(,
Sam O\'neal,
),

I've tried the following code:

string pattern = @"(<=|>=|!=|=|>|<|\)|\(|\s+)";
var tokens = new List<string>(Regex.Split(filter, pattern));
tokens.RemoveAll(x => String.IsNullOrWhiteSpace(x));

I'm not sure how to keep the string in single quotes as a one token. I'm new to Regex and would appreciate any help.

1 Answers
Related