如何在(匹配字母后的mystring)中查找匹配字母

本文关键字:mystring 查找 | 更新日期: 2023-09-27 17:58:38

我需要检查c#中的复杂属性。我得到的复杂属性字符串列表是:

   EmployeeID
   contactNo
   Employee.FirstName  // these is complex property
   Employee.LastName   //  these is complex property

我知道regex.match(),但我不知道如何在放入点值后签入字符串,这意味着我想在Employee和放入点值之后签入。你能帮我想一想吗?

如何在(匹配字母后的mystring)中查找匹配字母

使用regex,您可以匹配如下复杂属性:

List<string> properties = new List<string>()
{
    "EmployeeID",
    "contactNo",
    "Employee.FirstName",  // these is complex property
    "Employee.LastName",   //  these is complex property
};
Regex rgx = new Regex(@"Employee'.(.*)");
var results = new List<string>();
foreach(var prop in properties)
{
    foreach (var match in rgx.Matches(prop))
    {
        results.Add(match.ToString());
    }
}

如果您只想要.FirstNameLastName)之后的内容,请替换如下模式:

Regex rgx = new Regex(@"(?<=Employee'.)'w*");

不带正则表达式:

List<string> listofstring = { .... };
List<string> results = new List<string>();
const string toMatch = "Employee.";
foreach (string str in listofstring)
{
   if (str.StartsWith(toMatch))
   {
      results.Add(str.Substring(toMatch.Length));
   }
}

如果您只需要匹配.

List<string> listofstring = { .... };
List<string> results = new List<string>();
const string toMatch = ".";
int index = -1;
foreach (string str in listofstring)
{
   index = str.IndexOf(toMatch);
   if(index >= 0)
   { 
      results.Add(str.Substring(index + 1));
   }
}