搜索字符串"%"

Kai*_*Kai 3 .net c# regex .net-2.0 c#-2.0

我正在尝试编写一个ac#search函数,它支持SQL中的"%",就像搜索中的运算符一样.例如,

"%m%"将匹配以下所有字符串.

  • 一些
  • 我
  • 和
  • 等等..

"%m%e"将匹配类似"some"的字符串.

Rub*_*ias 5

static bool Like(string expression, string value)
{
    return Regex.IsMatch(value, "^" + 
        Regex.Escape(expression).Replace("%", @"[\s\S]*?") + "$", 
        RegexOptions.IgnoreCase);
}
Run Code Online (Sandbox Code Playgroud)