none
Analyzing sql string with regexp RRS feed

  • Question

  • 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'),

    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?

    Saturday, May 25, 2019 11:52 AM

All replies

  • Try this expression:

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

    Then extract the “values” group and examine its Captures collection:

    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 );
        }
    }
    

    Sunday, May 26, 2019 7:28 AM