2016-07-26 123 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)); 
    } 
}