且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式通配符

更新时间:2023-02-21 13:15:08

通配符 * 等同于正则表达式。*(贪婪)或。*?(不贪婪),所以你要执行与string.replace()第一个

The wildcard * is equivalent to the Regex pattern ".*" (greedy) or ".*?" (not-greedy), so you'll want to perform a string.Replace() first

string pattern = inputPattern.Replace("*", ".*?");
Regex regex = new Regex(pattern);



编辑:

请记住,如果 inputPattern 包含正则表达式中使用的任何特殊字符,你的模式将发生爆炸。

Keep in mind, that if inputPattern contains any special character used by Regex, your pattern would explode.

Regex.IsMatch(input, ".NET"); // may match ".NET", "aNET", "?NET", "*NET" and many more

所以你要留意他们(把 \ 这些,如的前面。 - > \

So you'll want to keep an eye out for them (put a \ in front of those, such as . -> \.)

MSDN:正则表达式快速参考