加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – “紧”重复键/值匹配

发布时间:2020-12-16 01:26:26 所属栏目:百科 来源:网络整理
导读:具有键/值对的“紧密”重复模式的字符串(对于此示例,键是“名称”,值应该是单个小写字) string text = "name: abc name: def name: ghi name: jkl"; 应该转换为输出 abc,def,ghi,jkl, 而在模式中的任何干扰(“非紧”所以说) string text = "name: abc x name
具有键/值对的“紧密”重复模式的字符串(对于此示例,键是“名称”,值应该是单个小写字)

string text = "name: abc name: def name: ghi name: jkl";

应该转换为输出

abc,def,ghi,jkl,

而在模式中的任何干扰(“非紧”所以说)

string text = "name: abc x name: def name: ghi name: jkl";

应该导致比赛失败,这有点像

abc,## Exception occurred: x cannot be matched to the pattern ##

我试过了

string text = "name: abc name: def name: ghi name: jkl";
string pattern = @"name:s*([a-z])*s*";

MatchCollection ms = Regex.Matches(text,pattern);

foreach (Match m in ms)
{
    Console.Write(m.Groups[1].Value+",");
}

但它回来了

c,f,i,l,

是什么导致了这种奇怪的行为,我该如何解决?

解决方法

与大多数其他正则表达式不同,C#(.Net)的引擎实际上通过Group类的 Captures属性跟踪重复捕获.

Group.Captures Property

Gets a collection of all the captures matched by the capturing group,in innermost-leftmost-first order (or innermost-rightmost-first order if the regular expression is modified with the 07001 option).

这意味着通过访问Groups [1](如下面的代码所示)然后访问Captures属性,我们有效地获取每个重复捕获的值在我们的字符串上.

See code in use here

using System;
using System.Linq;
using System.Text.RegularExpressions;

class Example {

    static void Main() {
        string[] strings = new string[]{
            "name: abc name: def name: ghi name: jkl","name: abc x name: def name: ghi name: jkl"
        };
        Regex regex = new Regex(@"^(?:name: *([a-z]+) *)+$");
        foreach(string s in strings) {
            if(regex.IsMatch(s)) {
                Match match = regex.Match(s);
                Console.WriteLine(string.Join(",",from Capture c in match.Groups[1].Captures select c.Value));
            } else {
                Console.WriteLine("Invalid input");
            }
        }
    }
}

结果

name: abc name: def name: ghi name: jkl     # abc,jkl
name: abc x name: def name: ghi name: jkl   # Invalid input

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读