Regular expression for splitting a semicolon string with double quotes in C #

I tried using regex to separate a comma and space string. The expression matches all but one of the cases. The code I tried is:

        List<string> strNewSplit = new List<string>();
        Regex csvSplit = new Regex("(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)", RegexOptions.Compiled);
        foreach (Match match in csvSplit.Matches(input))
        {
            strNewSplit.Add(match.Value.TrimStart(','));
        }
        return strNewSplit;

CASE1: "MYSQL, ORACLE", "C #, ASP.NET"

EXpectedOutput:

"MYSQL, ORACLE"

"C #, ASP.NET"

RESULT: PASS

CASE2: "MYSQL, ORACLE", "C #, ASP.NET"

ExpectedOutput:

"MYSQL, ORACLE"

"C #, ASP.NET"

Actual output:

"MYSQL, ORACLE"

"WITH#

ASP.NET "

RESULT: FAIL.

If I provided a decimal space between two DoubleQuotes, I did not get the corresponding output. Did I miss something? Please provide the best solution.

+3
source share
1 answer

EBNF .

:

List = ListItem { *, * ListItem} *;

ListItem = "" "" ";// -

= [\ t] +;

, List ListItem, mutliple (*) ListItems, .

( ListItems):

static void Main(string[] args)
{
    matchRegex("\"MYSQL,ORACLE\",\"C#,ASP.NET\"").ForEach(Console.WriteLine);
    matchRegex("\"MYSQL,ORACLE\", \"C#,ASP.NET\"").ForEach(Console.WriteLine);
}
static List<string> matchRegex(string input)
{
    List<string> strNewSplit = new List<string>();
    Regex csvSplit = new Regex(
        "(\"(?:[^\"]*)\")"
        , RegexOptions.Compiled);
    foreach (Match match in csvSplit.Matches(input))
    {
       strNewSplit.Add(match.Value.TrimStart(','))
    }
    return strNewSplit;
}

, . , .

+1

All Articles