C# C LINQ:如何处理通配符搜索?
我需要这样的搜索功能: 该函数接受具有多种通配符类型的所有字符串:C# C LINQ:如何处理通配符搜索?,c#,linq,wildcard,C#,Linq,Wildcard,我需要这样的搜索功能: 该函数接受具有多种通配符类型的所有字符串: * or % >>> can replace one or more characters # >>> used to replace a number @ >>> used to replace an alphabet ? >>> used to replace a character (can be both number and alphabet)
* or % >>> can replace one or more characters
# >>> used to replace a number
@ >>> used to replace an alphabet
? >>> used to replace a character (can be both number and alphabet)
据我所知,在LINQ中有3个用于搜索字符串的函数:StartsWith、EndsWith和Contains。在SQL查询中,对于一个或多个字符,有两种类型的LIKE:%s和u。
那么,我该如何解决这个问题呢
非常感谢您可以使用正则表达式并用正则表达式替换通配符。这里有一个例子
IEnumerable<string> Search(IEnumerable<string> data, string q)
{
string regexSearch = q
.Replace("*", ".+")
.Replace("%", ".+")
.Replace("#", "\\d")
.Replace("@", "[a-zA-Z]")
.Replace("?", "\\w");
Regex regex = new Regex(regexSearch);
return data
.Where(s => regex.IsMatch(s));
}
您可以使用正则表达式并用正则表达式替换通配符。这里有一个例子
IEnumerable<string> Search(IEnumerable<string> data, string q)
{
string regexSearch = q
.Replace("*", ".+")
.Replace("%", ".+")
.Replace("#", "\\d")
.Replace("@", "[a-zA-Z]")
.Replace("?", "\\w");
Regex regex = new Regex(regexSearch);
return data
.Where(s => regex.IsMatch(s));
}
我需要一个像这样的搜索功能,那么你到目前为止尝试实现了什么?你能提供一些样本数据吗?我需要一个像这样的搜索功能,那么你到目前为止尝试实现了什么?你能提供一些样本数据吗?