简体   繁体   English

C#正则表达式提取字符串括在单引号中

[英]C# regex extract string enclosed into single quotes

I've the following string that I need to parse using RegEx. 我需要使用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: 这是一个SQL过滤器,我想使用以下分隔符将其拆分为标记:

(, ), >,<,=, 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. 我是Regex的新手,非常感谢任何帮助。

Your pattern needs an update with yet another alternative branch: '[^'\\\\]*(?:\\\\.[^'\\\\]*)*' . 您的模式需要使用另一个替代分支进行更新: '[^'\\\\]*(?:\\\\.[^'\\\\]*)*'

It will match: 它将匹配:

  • ' - a single quote ' - 单引号
  • [^'\\\\]* - 0+ chars other than ' and \\ [^'\\\\]* - 除了'\\之外' 0+字符
  • (?: - a non-capturing group matching sequences of: (?: - 非捕获组匹配序列:
    • \\\\. - any escape sequence - 任何逃脱序列
    • [^'\\\\]* - 0+ chars other than ' and \\ [^'\\\\]* - 除了'\\之外' 0+字符
  • )* - zero or more occurrences )* - 零次或多次出现
  • ' - a single quote ' - 单引号

In C#: 在C#中:

string pattern = @"('[^'\\]*(?:\\.[^'\\]*)*'|<=|>=|!=|=|>|<|\)|\(|\s+)";

See the regex demo 请参阅正则表达式演示

C# demo : C#demo

var filter = @"abc = 'def' and size = '1 x(3"" x 5"")' and (name='Sam O\'neal')";
var pattern = @"('[^'\\]*(?:\\.[^'\\]*)*'|<=|>=|!=|=|>|<|\)|\(|\s+)";
var tokens = Regex.Split(filter, pattern).Where(x => !string.IsNullOrWhiteSpace(x));
foreach (var tok in tokens)
    Console.WriteLine(tok);

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM