c# regex matches example

后端 未结 6 2123
耶瑟儿~
耶瑟儿~ 2020-12-01 11:46

I am trying to get values from the following text. How can this be done with Regex?

Input

Lorem ipsum dolor sit %download%#456 amet, consect

6条回答
  •  借酒劲吻你
    2020-12-01 12:18

    All the other responses I see are fine, but C# has support for named groups!

    I'd use the following code:

    const string input = "Lorem ipsum dolor sit %download%#456 amet, consectetur adipiscing %download%#3434 elit. Duis non nunc nec mauris feugiat porttitor. Sed tincidunt blandit dui a viverra%download%#298. Aenean dapibus nisl %download%#893434 id nibh auctor vel tempor velit blandit.";
    
    static void Main(string[] args)
    {
        Regex expression = new Regex(@"%download%#(?[0-9]*)");
        var results = expression.Matches(input);
        foreach (Match match in results)
        {
            Console.WriteLine(match.Groups["Identifier"].Value);
        }
    }
    

    The code that reads: (?[0-9]*) specifies that [0-9]*'s results will be part of a named group that we index as above: match.Groups["Identifier"].Value

提交回复
热议问题