Regexp&回溯参考

时间:2011-01-11 08:35:33

标签: .net regex

专家 我需要帮助从字符串中提取

“AAA,BBB”,“CCC”,DDDD

以下小组:

  1. AAA,BBB
  2. CCC
  3. DDDD
  4. 是否可以通过正则表达式提取三组,如果是,那么如何?

    感谢。

2 个答案:

答案 0 :(得分:0)

功能

public void RunTest()
{
    const string toTest = "\"AAA, BBB\", \"CCC\", \"DDDD\"";
    var exp = new Regex("\G(?:^|,)\s*\"([^\"])\"");
    var matches = exp.Matches(toTest);
    foreach (var match in matches.Cast())
    {
        Console.WriteLine(@"Matched expression: {0}", match);
        foreach (var group in match.Groups.Cast())
        {
            Console.WriteLine(@"Matched group: {0}", group);
        }
    }
}
将返回
Matched expression: "AAA, BBB"
Matched group: "AAA, BBB"
Matched group: AAA, BBB
Matched expression: , "CCC"
Matched group: , "CCC"
Matched group: CCC
Matched expression: , "DDDD"
Matched group: , "DDDD"
Matched group: DDDD
因此收集每一个第二组你得到我想你想要的东西。 请注意,我在您的DDDD周围添加了双引号。我以为这是一个错字。 如果它不是拼写错误,你可以试试正则表达式:
var exp = new Regex("\G(?:^|,)(?:\s(?:\"([^\"])\")|([^\",]))");

说明:

\G The match must occur at the point where the previous match ended.
[^"] Any character except the double quote
\s Any whitespace
* zero or more occurrences of the preceding element
( and ) define a group
(?: defines a noncapturing group
希望有所帮助:)

答案 1 :(得分:0)

var delimiterPattern = @",(?=(?:[^\"]\"[^\"]\")(?![^\"]\"))"; var parts = string.Split(delimiterPattern);

for string:玩具“r”us“,”AAAA“,”玩具“r”us“,”玩具,r“,”玩具“,r”,“,测试

将返回: 1.“玩具”r“我们” 2.“AAAA” 3.“玩具”r“我们” 4.“玩具,r,” 5.“玩具”,“r”,“ 6.测试