0

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:

enter image description here

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?

1
  • Well, do you understand the error? FindAll is expecting a predicate - something that returns true or false. match.Match(str).Value returns a string. Perhaps you were looking for ConvertAll? (Or just use Select and LINQ...) Commented Feb 24, 2017 at 21:48

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean? I thought the predicate was supposed to "essentially" assert the regex against each index in the first list, and assign that match to a spot in the new list.
Nope, check this msdn.microsoft.com/en-us/library/bfcke1bz(v=vs.110).aspx It will explain about Predicate class. FindAll is more like Where but they are slightly different in term of execution and performance. If you want to "transform" your collection then select.
Ah, I see. I'm still familiarizing myself with the System.Linq library for .NET, so thank you for the pointers!
1

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.