Learning Learning - 3 months ago 8
C# Question

Trying to find word in string but getting enumeration yields no result

I am having 1 long string in which i want to find word starting with

Emp
only after dot in my string and if match then extract that part after dot.

Below is my string:

Value.EmployeeRFID,Value.EmployeeRFID1,Value.EmkhasisGFTR,Value.EmployeeGHID,Value.EmployeeFCKJ


Now in my above input i just want to extract only part after dot(
Foreg:EmployeeRFID,EmployeeRFID1 etc
) and i want to add that in below list:

var list= new List<string>();


Expected output in above list variable:

[0]:EmployeeRFID ,[1]=EmployeeRFID1, [2]:EmployeeGHID, [3]:EmployeeFCKJ


This is how i am trying with linq but i am getting
Enumeration yielded no results
:

string str="Value.EmployeeRFID,Value.EmployeeRFID1,Value.EmkhasisGFTR,Value.EmployeeGHID,Value.EmployeeFCKJ";

var data= str.Where(t=>t.ToString().StartsWith("Emp")).Select(t=>t.t.ToString()) // Enumeration yielded no results

Answer

Try regular expressions:

  string source = "Value.EmployeeRFID,...,Value.EmployeeGHID,Value.EmployeeFCKJ";
  string pattern = @"(?<=\.)Emp\w*";

  string[] result = Regex.Matches(source, pattern, RegexOptions.IgnoreCase)
    .OfType<Match>()
    .Select(match => match.Value)
    .ToArray();

Test:

  // EmployeeRFID
  // EmployeeRFID1
  // EmployeeGHID
  // EmployeeFCKJ
  Console.Write(string.Join(Environment.NewLine, result));