3

I have tried a regular expression to split a string with comma and space. Expression matches all the cases except only one. The code I have 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"

"C#

ASP.NET"

RESULT: FAIL.

If I provide a space after a comma in between two DoubleQuotes then I didn't get appropriate output. Am I missing anything? Please provide a better solution.

4

1 回答 1

1

我通常写下我的输入的 EBNF 来解析。

在你的情况下,我会说:

List = ListItem {Space* , Space* ListItem}*;

ListItem = """ 标识符 """; // 标识符就是一切“

空格 = [\t]+;

这意味着一个 List 由一个 ListItem 组成,该 ListItem 后跟零个或多个 (*) ListItems,这些 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;
}

哪个返回你想要的。希望我理解正确。

于 2012-05-17T06:20:37.230 回答