【发布时间】:2011-08-11 16:13:13
【问题描述】:
这是我的源字符串:
<box><3>
<table><1>
<chair><8>
这是我的正则表达式模式:
<(?<item>\w+?)><(?<count>\d+?)>
这是我的物品类
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);
}
可能会出现一些小错误,例如在这个示例中缺少字母,因为这是我在应用程序中的更简单版本。
问题是最后我在 OrderList 中只有一个 Item。
更新
我让它工作了。 谢谢帮忙。
【问题讨论】:
-
刚刚运行 - 工作正常(列表中的 3 项)。
-
可以分享一下吗?如果有人遇到同样的问题,可能会有所帮助。
-
@ChrisWue 这是我的应用程序代码中的错误。没有帮助。
-
一些关于理解和访问正则表达式匹配的代码可以在stackoverflow.com/a/27444808/546871找到