正则表达式溢出字符串与匹配组的出现

时间:2018-12-18 17:35:26

标签: c# regex

字符串为“印度德里,英国伦敦,美国纽约(洛杉矶除外),欧洲法国(巴黎除外)”

我想用给定的字符串来溢出结果,如下所示:

`string str ="India Delhi, UK London, US Newyork (except Los Angeles), Europe France (except Paris)"
 match.Groups[1].Value = "India Delhi, UK London, US Newyork"
 match.Groups[2].Value = "Los Angeles"
 match.Groups[3].Value = "Europe France"
 match.Groups[4].Value = "Paris"`

对此有何线索?

2 个答案:

答案 0 :(得分:1)

这是一个有点奇怪的要求,但是此正则表达式可以满足您的要求。匹配和捕获文本比分割要好,因为您的预期输出会显示一些部分被输入文本截断。

firebase.initializeApp({
    credentials: admin.credential.cert(<<path-to-your-certificate> || <admin.ServiceAccount object>>),
    databaseURL: <my db url here>
});

Demo

这是C#示例代码,

e[which(e$obs_pval==min(e$obs_pval)),]
              snp     obs_pval
1    1.852962e-07 1.852962e-07
2174 4.971520e+07 1.852962e-07

将按预期打印输出,

[A-Z][\w ]+(?:, [\w ]+)*

答案 1 :(得分:-3)

以下linq应该可以工作

        static void Main(string[] args)
        {
            List<List<string>> groups = new List<List<string>>() {
                new List<string>() { "India Delhi", "UK London", "US Newyork"},
                new List<string>() { "Los Angeles" },
                new List<string>() { "Europe France"},
                new List<string>() { "Paris"}
            };


            string input = "India Delhi, UK London, US Newyork, Europe France";

            var results = input.Split(new char[] { ',' }).Select(x => x.Trim()).SelectMany(x => groups.Select((y, i) => new { str = x, truth = y.Contains(x), group = i }))
                .Where(x => x.truth == true)
                .GroupBy(x => x.group)
                .Select(x => x.Select(y => y.str).ToList())
                .ToList();
        }