Reputation: 1009
I've a little test program that builds a List
of different strings, all of which contain the same formatted number. I also then declare another list which is supposed to contain the specific numbers of each string in the former list.
My plan is to accomplish this by utilizing a regular expression match inside of a lambda function.
Every time I try and do this I get the following error:
List<string> newList = new List<string>(new string[] { "MyName - v 3.7.5.0 ... CPU:",
"MyName - v ... CPU: - 1.5.7.2",
"4.21.66.2 - v ... CPU:",
" - v ... CPU: 31.522.9.0" });
Regex match = new Regex("(\\d+\\.)+\\d");
List<string> otherList = newList.FindAll(str => match.Match(str).Value);
Is there any way I can use lambda functions to accomplish this?
Upvotes: 0
Views: 8346
Reputation: 3188
List<string> newList = new List<string>(new string[] { "MyName - v 3.7.5.0 ... CPU:",
"MyName - v ... CPU: - 1.5.7.2",
"4.21.66.2 - v ... CPU:",
" - v ... CPU: 31.522.9.0" });
Regex match = new Regex("(\\d+\\.)+\\d");
var result = match.Matches(string.Join(" ", newList)).Cast<Match>().Select(m => m.Value);
Upvotes: 0
Reputation: 1096
you can try the following:
var otherList = newList.Select(str => match.Match(str).Value);
FindAll expects a Predicate, so you would need to do:
newList.FindAll(str => match.IsMatch(str));
But then you would have an IEnumerable
that would contain the full strings and not just the numbers you are looking for.
Upvotes: 1
Reputation: 3208
You can try this:
List<string> otherList = newList.Select(str => match.Match(str).Value).ToList();
Btw, your code is failing because the predicate is expecting bool
.
Upvotes: 7