Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This is my source string:

<box><3>
<table><1>
<chair><8>

This is my Regex Patern:

<(?<item>w+?)><(?<count>d+?)>

This is my Item class

class Item
{
    string Name;
    int count;
    //(...)
}

This is my Item Collection;

List<Item> OrderList = new List(Item);

I want to populate that list with Item's based on source string. This is my function. It's not working.

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);
            }

Threre might be some small mistakes like missing letter it this example because this is easier version of what I have in my app.

The problem is that In the end I have only one Item in OrderList.

UPDATE

I got it working. Thans for help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
159 views
Welcome To Ask or Share your Answers For Others

1 Answer

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();
    }
}

Returns 3 matches for me. Your problem must be elsewhere.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...