Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过正则表达式匹配循环_C#_.net_Regex_Foreach - Fatal编程技术网

C# 通过正则表达式匹配循环

C# 通过正则表达式匹配循环,c#,.net,regex,foreach,C#,.net,Regex,Foreach,这是我的源字符串: <box><3> <table><1> <chair><8> 这是我的物品收藏 List<Item> OrderList = new List(Item); List OrderList=新列表(项目); 我想用基于源字符串的项填充该列表。 这是我的职责。它不起作用了 Regex ItemRegex = new Regex(@"<(?<item>\w+?)><

这是我的源字符串:

<box><3>
<table><1>
<chair><8>
这是我的物品收藏

List<Item> OrderList = new List(Item);
List OrderList=新列表(项目);
我想用基于源字符串的项填充该列表。 这是我的职责。它不起作用了

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);
            }
Regex ItemRegex=newregex(@“”,RegexOptions.Compiled);
foreach(在ItemRegex.Matches(sourceString)中匹配itemratch)
{
Item temp=新项目(ItemMatch.Groups[“Item”].ToString(),int.Parse(ItemMatch.Groups[“count”].ToString());
OrderList.Add(临时);
}
这可能是一些小错误,比如在本例中遗漏了字母it,因为这是我在应用程序中拥有的更简单的版本

问题是,最终我在OrderList中只有一项

更新

我让它工作了。 谢谢你的帮助。

课程计划
class Program
{
    static void Main(string[] args)
    {
        string sourceString = @"<box><3>
<table><1>
<chair><8>";
        Regex ItemRegex = new Regex(@"<(?<item>\w+?)><(?<count>\d+?)>", RegexOptions.Compiled);
        foreach (Match ItemMatch in ItemRegex.Matches(sourceString))
        {
            Console.WriteLine(ItemMatch);
        }

        Console.ReadLine();
    }
}
{ 静态void Main(字符串[]参数) { 字符串sourceString=@“ "; Regex ItemRegex=newregex(@“”,RegexOptions.Compiled); foreach(在ItemRegex.Matches(sourceString)中匹配itemratch) { Console.WriteLine(ItemMatch); } Console.ReadLine(); } }

为我返回3个匹配项。您的问题一定在其他地方。

为了将来的参考,我想将上述代码转换为使用声明性方法作为LinqPad代码片段的文档:

var sourceString = @"<box><3>
<table><1>
<chair><8>";
var count = 0;
var ItemRegex = new Regex(@"<(?<item>[^>]+)><(?<count>[^>]*)>", RegexOptions.Compiled);
var OrderList = ItemRegex.Matches(sourceString)
                    .Cast<Match>()
                    .Select(m => new
                    {
                        Name = m.Groups["item"].ToString(),
                        Count = int.TryParse(m.Groups["count"].ToString(), out count) ? count : 0,
                    })
                    .ToList();
OrderList.Dump();
var sourceString=@”
";
var计数=0;
var ItemRegex=newregex(@“]+)>]*)>”,RegexOptions.Compiled);
var OrderList=ItemRegex.Matches(sourceString)
.Cast()
.选择(m=>new
{
Name=m.Groups[“item”].ToString(),
Count=int.TryParse(m.Groups[“Count”].ToString(),out Count)?计数:0,
})
.ToList();
Dump();
输出:


刚刚运行-工作正常(列表中有3项)。您能分享吗?如果某人遇到同样的问题,他可能会有所帮助。@ChrisWue这是我的应用程序代码中的错误。没有帮助。一些关于理解和访问正则表达式匹配的代码可以在屏幕截图上的哪个程序中找到?这是您自己的程序吗?它是由LinqPad的Dump()扩展方法生成的。您可以将Dump()粘贴在大多数对象的末尾,它将输出对象的格式化表示形式。LinqPad只是一个编写/评估C#代码的特殊工具。上面的代码可以直接复制粘贴到LinqPad中,它将生成表。。。我要点击“编辑”来查看制作如此漂亮表格的降价。
class Program
{
    static void Main(string[] args)
    {
        string sourceString = @"<box><3>
<table><1>
<chair><8>";
        Regex ItemRegex = new Regex(@"<(?<item>\w+?)><(?<count>\d+?)>", RegexOptions.Compiled);
        foreach (Match ItemMatch in ItemRegex.Matches(sourceString))
        {
            Console.WriteLine(ItemMatch);
        }

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