的直接的方式將有if
陳述依次測試每個正則表達式的序列:
if (Regex.Match(query, regex1)) {
HandleFirstCase(query);
} else if(Regex.Match(query, regex2)) {
HandleSecondCase(query);
} ...
else {
HandleFullTextSearch();
}
更復雜的方式將是一個數據驅動的方法,在表格中存儲正則表達式和動作並循環播放它們的地方:
public class RegexAction {
public Regex Pattern { get; private set; }
public Action<string> Handler { get; private set; }
public RegexAction(Regex pattern, Action<string> handler) {
this.Pattern = pattern;
this.Handler = handler;
}
}
public static RegexAction[] HandlerTable = new RegexAction[] {
new RegexAction(regex1, HandleFirstCase),
new RegexAction(regex2, HandleSecondCase),
new RegexAction(regex3, HandleThirdCase),
// ...
}
foreach(RegexAction action in HandlerTable) {
if (action.Match(query)) {
action.Handler(query);
break;
}
}
這有助於將imp從測試和調用的實施中提取數據,模式和行爲。
我認爲對於現狀,我想有一個選擇的形式,因爲它似乎只接受一定的價值觀。你爲什麼不顯示四個不同的輸入表單,並使用所有已填寫的表單,選擇用於查詢?這樣,你也可以進行組合查詢。 – Svante 2008-11-03 16:22:03