使用regexp分析sql字符串 [英] Analyzing sql string with regexp

查看:99
本文介绍了使用regexp分析sql字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分析一个导出的sql文件。

I want to analyze an exported sql file.

这意味着我要转换一个字符串,如

That means that I want to convert a string like

(80, NULL,57,'DIRECT_TRAFFIC','40','0000-00-00 00:00:00'),

(80, NULL, 57, 'DIRECT_TRAFFIC', '40', '0000-00-00 00:00:00'),

到一个数组中,其中六个值中的每一个都是一个条目。

into an array where each of the six values is one entry.

我陷入了变化(有,没有引号,空值等)。有没有人知道一个好的公式?

I get stuck in the variations (with, without quote, null values, etc). Does anyone know a good formula?

推荐答案

试试这个表达式:

   \(\s *(:( '值' '(:???[^'] | '')* '| [^ [' \s)] +)(?:\\ \\ * *,\ s * |))+ \ * * \)

   \(\s*(?:(?'values''(?:[^']|'')*'|[^[,'\s)]+)(?:\s*,\s*|))+\s*\)

然后提取"values"组并检查其 Captures 集合:

string example = " some text (80, NULL, 57, 'DIRECT_TRAFFIC', '40', '0000-00-00 00:00:00') some text";
string pattern = @"\(\s*(?:(?'values''(?:[^']|'')*'|[^[,'\s)]+)(?:\s*,\s*|))+\s*\)";

foreach( Match match in Regex.Matches( example, pattern ) )
{
    foreach( Capture capture in match.Groups["values"].Captures )
    {
        Console.WriteLine( capture.Value );
    }
}


这篇关于使用regexp分析sql字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆