Reputation:
I know that there is always only one element in MatchCollection
:
Regex reg = new Regex("1234");
MatchCollection matches = reg.Matches("fjasij 1234 gdsgds");
Console.WriteLine(matches.Count);
string s = ?;
How to assign this one element to the variable string s
without foreach
loop?
Upvotes: 6
Views: 4177
Reputation: 65077
Why use a MatchCollection
then? Just get a single Match
:
var match = reg.Match("fjasij 1234 gdsgds");
Upvotes: 13
Reputation: 6270
string s = matches[0];
Note that this will fail if you have zero matches.
Upvotes: 6