大厦马丁布特内尔的答案,这是我做的VB Like
运营商转换成C#Regex
。由于Visual Studio 2013中的Like
运算符支持一些额外的正则表达式功能,因此转换该模式需要额外的工作。
// escape RegEx special characters
var pattern = Regex.Escape(likePattern);
// convert negative character lists into RegEx negative character classes
pattern = Regex.Replace(pattern, @"\\\[!(?<c>[^\]]*)\\\]", @"[^${c}]", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
// convert positive character lists into RegEx positive character classes
pattern = Regex.Replace(pattern, @"\\\[(?<c>[^\]]*)\\\]", @"[${c}]", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
// convert asterisks into RegEx pattern for zero or more characters
pattern = Regex.Replace(pattern, @"(?<!\[[^\]]*)\\*(?![^\]*\])", @".*", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
// convert question marks into RegEx pattern for any single character
pattern = Regex.Replace(pattern, @"(?<!\[[^\]]*)\\?(?![^\]*\])", @".", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
// convert hash/number sign into RegEx pattern for any single digit (0 - 9)
pattern = Regex.Replace(pattern, @"(?<!\[[^\]]*)#(?![^\]*\])", @"[0-9]", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
// make pattern match whole string with RegEx start and end of string anchors
pattern = @"^" + pattern + @"$";
// perform "like" comparison
return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
警告:此代码尚未经过测试!
那么,如果你知道这是做什么,那么你为什么不告诉我们? :) –
因此,它匹配一个文字'*',任意14个任意字符和一个文字'8'?这是我从[这里]得到的(http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx) –