Looping through Regex Matches

前端 未结 2 1339
一整个雨季
一整个雨季 2020-12-09 08:05

This is my source string:

<3>
<1>
<8>


This is my Regex Patern:

<(         


        
      
      
      
2条回答
  •  执念已碎
    2020-12-09 08:16

    For future reference I want to document the above code converted to using a declarative approach as a LinqPad code snippet:

    var sourceString = @"<3>
    
<1> <8>"; var count = 0; var ItemRegex = new Regex(@"<(?[^>]+)><(?[^>]*)>", RegexOptions.Compiled); var OrderList = ItemRegex.Matches(sourceString) .Cast() .Select(m => new { Name = m.Groups["item"].ToString(), Count = int.TryParse(m.Groups["count"].ToString(), out count) ? count : 0, }) .ToList(); OrderList.Dump();

With output:

List of matches

提交回复
热议问题