Running regex on String using Linq

I have a String, I want to use Linq to run a regex to shorten my string to a smaller substring that matches my ex case.

My code is currently giving an error

'char' does not contain a definition for 'Name' and no extension to the method 'Name' that takes the first argument of type 'char' can be found

My code is:

string variable = result.Name.Select(r => regEx.Match(r.Name).Groups[2].ToString());

Result.Name is the string contained in the user class.

What did I do wrong? What is wrong with my syntax / understanding?

+3
source share
4 answers

Select . Select , r - char.

, , , LINQ.

string variable = regEx.Match(result.Name).Groups[2].ToString();

( , result.Name - , .)

+2

- ?

string[] result = Regex.Matches(input, pattern)
                       .Cast<Match>()
                       .Select(match => match.Groups[2].Value)
                       .ToArray();
+12

Must not be

string variable = result.Select(r => regEx.Match(r.Name).Groups[2].ToString());

if you run Select in the Name field, you run it in a string that is a char array

0
source

This should work ...

string result = result.Name.Select(@regEx => Regex.Match(match.Groups[2].ToString(), @regEx));
0
source

All Articles