2016-07-26 8 views
-1

私はC#で複合プロパティをチェックする必要があります。私は(regex.matchについて知っマッチレターの検索方法(マッチレター後のミストリング)

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

)しかし、私は後のドット値に配置された内の文字列を確認する方法について疑問を持っている私は、従業員に確認したいということと:私は、文字列の複雑なプロパティリストをしている得ます配置されたドットの値の後にこれについて何か考えてもらえますか?正規表現を使用して

答えて

1

、あなたはこのような複雑な特性を一致させることができます:あなただけ.FirstNameLastName)の後にあるものをしたい場合は

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

を、このようなパターンに置き換えます。

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

Ifあなたはちょうど一致する必要があります.

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