1 回答

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊
在將字符串與模式匹配之前,請(qǐng)將通配符更改為與任何內(nèi)容匹配的字符。像這樣:
// this method change W wild-card character to a character that match anything
private static bool MatchToString(string stringToMatch, string pattern) {
Regex r = new Regex(pattern.Replace("W", "."));
return r.Match(stringToMatch).Success;
}
static void Main() {
// these are matches
Console.WriteLine(MatchToString("HHH", "HWW"));
Console.WriteLine(MatchToString("HHH", "HHW"));
Console.WriteLine(MatchToString("WWW", "WWW"));
Console.WriteLine(MatchToString("HHWH", "WWWW"));
//these are doesn't
Console.WriteLine(MatchToString("HHH", "HHK"));
Console.WriteLine(MatchToString("HHH", "HKK"));
Console.WriteLine(MatchToString("WWW", "ABC"));
Console.ReadLine();
}
- 1 回答
- 0 關(guān)注
- 108 瀏覽
添加回答
舉報(bào)