2016-07-26 132 views
-1

我需要检查c#中的复杂属性。我得到的字符串的复杂属性列表是:如何查找匹配字母(匹配字母后的mystring)

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

我知道regex.match(),但我有疑问,有关如何检查字符串中放置的点值,这意味着我要在员工检查后放置点值后。你能帮助解决这个问题吗?

回答

1

使用正则表达式,你可以像本场比赛的复杂性:

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

无正则表达式:

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