Reputation: 5
I wanted to use Regex to get data from repeated XML tag:
<A>cat</A><A>dog</A>
So I've created Regex:
<A>(.*?)</A>
and code:
string text = "<A>asdasd</A><A>dsfsd</A>";
string regex = @"<A>(.*?)</A>";
Regex rgx = new Regex(regex);
Match match = rgx.Match(text);
while(match.Success)
{
i++;
Console.WriteLine(match.Groups[1].Value);
match.NextMatch();
}
But when I start my code, my loop is endless and never stop.
Can someone help me find what's wrong with code? Or find another solution? (I don't want to deserialize XML).
Upvotes: 0
Views: 323
Reputation: 3576
Just use Regex.Matches to load them all into a collection, and proceed to iterate it.
string text = "<A>asdasd</A><A>dsfsd</A>";
string regex = @"<A>(.*?)</A>";
foreach (Match m in Regex.Matches(text, regex))
{
Console.WriteLine(m.Groups[1].Value);
}
Or single line using Linq:
Regex.Matches(text, regex).Cast<Match>().ToList().ForEach(m => Console.WriteLine(m.Groups[1].Value));
Upvotes: 1
Reputation: 626747
While the failure reason is that you did not assign the next match to match
, you can actually use Regex.Matches
to get all the substrings you need in one go without the need for an explicit loop:
var results = rgx.Matches(text)
.Cast<Match>()
.Select(m => m.Groups[1].Value);
Console.WriteLine(string.Join("\n", results));
See the C# online demo:
var text = "<A>asdasd</A><A>dsfsd</A>";
var regex = @"<A>(.*?)</A>";
var rgx = new Regex(regex);
var results = rgx.Matches(text)
.Cast<Match>()
.Select(m => m.Groups[1].Value);
Console.WriteLine(string.Join("\n", results));
// asdasd
// dsfsd
Upvotes: 1
Reputation: 32202
This:
match.NextMatch();
just returns the next match, it doesn't change the state of match
itself. You need to update the variable:
match = match.NextMatch();
Upvotes: 7