这是我的源字符串:
<box><3>
<table><1>
<chair><8>
这是我的正则表达式模式:
<(?<item>\w+?)><(?<count>\d+?)>
这是我的 Item 类
class Item
{
string Name;
int count;
//(...)
}
这是我的物品收藏;
List<Item> OrderList = new List(Item);
我想根据源字符串填充列表中的项目。这是我的函数,但它不起作用。
Regex ItemRegex = new Regex(@"<(?<item>\w+?)><(?<count>\d+?)>", RegexOptions.Compiled);
foreach (Match ItemMatch in ItemRegex.Matches(sourceString))
{
Item temp = new Item(ItemMatch.Groups["item"].ToString(), int.Parse(ItemMatch.Groups["count"].ToString()));
OrderList.Add(temp);
}
这个例子可能会有一些小错误,比如缺少字母,因为这只是我在应用程序中的简化版本。
问题是最后我只有一个订单列表中的项目。
更新
我解决了它。 感谢帮助。