왜,
var dic = Dictionary<Regex, MyClass>()
dic.Add(new Regex("..."), new MyClass)
....
foreach(var match in dic.Keys.Where(k => k.IsMatch(str)))
{
var myClass = dic[match];
....
}
이제 문제는 아마도 Match
라는 Predicate
로, 문자열 자체에 일치합니다. 당신은 단지 첫 경기를하려면
var matchers = new HashSet<MyClass>();
matchers.Add(new MyClass("some regex?");
....
foreach(var match in matchers.Where(Match(str)))
{
....
}
편집
당신은 FirstOrDefault
대신 Where
사용할 수 있습니다.
var firstMatch = matchers.FirstOrDefault(Match(str))
if (firstMatch != null)
{
....
}
그러나 목록의 순서가 중요합니다.
MyClass
의 부분적인 구현이 Match
술어가 될 수 iclude하는 2
편집 ...
partial class MyClass
{
private readonly RegEx matcher;
public MyClass(string regEx)
{
matcher = new RegEx(regEx);
}
public bool Match(string value)
{
return matcher.IsMatch(value);
}
}
정확히이 wildecards 정의하고 어떻게 그들이 작동 기대합니까? – Oded
키가 표준 정규식 형식을 따르도록하면 좋을 것입니다. 당신은 그 시나리오에서 LINQ + regex도 활용할 수있을 것입니다. – code4life
@Oded 나는 임의의 Dictionay Key를 문자열 (str)과 일치시키고 싶다. – Makah