Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何像这样解析搜索查询字符串_C#_Sql_Regex_Entity Framework_Parsing - Fatal编程技术网

C# 如何像这样解析搜索查询字符串

C# 如何像这样解析搜索查询字符串,c#,sql,regex,entity-framework,parsing,C#,Sql,Regex,Entity Framework,Parsing,我想建立一个搜索功能与关键字格式的实体框架 void funcSearch(string keywork) { if (keywork == "[tag]") { //regex for is tag //do search tag } if (keywork == "user:1234") {

我想建立一个搜索功能与关键字格式的实体框架

void funcSearch(string keywork)
        {
            if (keywork == "[tag]")
            {
                //regex for is tag
                //do search tag
            }
            if (keywork == "user:1234")
            {
                //regex for userid is 1234
                //do search user with 1234
            }
            ...
        }
我可以使用正则表达式来解析这样的查询字符串格式或任何方法吗?是否有一个函数可以使用相应的关键字分析所有案例

tags    [tag]
exact   "words here"
author  user:1234
user:me (yours)
score   score:3 (3+)
score:0 (none)
answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
views   views:250
sections    title:apples
body:"apples oranges"
url url:"*.example.com"
favorites   infavorites:mine
infavorites:1234
status  closed:yes
duplicate:no
migrated:no
wiki:no
types   is:question
is:answer

谢谢你的建议。

是的,你可以。您必须创建一个正则表达式列表来检查和循环它们,直到找到匹配项为止。(请确保正确排列它们的优先级。)

例如,要确定搜索查询是否正在查询标记,可以使用以下正则表达式:

string query = "[tag]";
bool isTag = Regex.IsMatch(query, @"^\[.+?\]$");
下面是另一个与用户ID匹配的正则表达式:

string query = "user:1234";
var match = Regex.Match(query, @"^user:(\d+)$", RegexOptions.IgnoreCase);

请注意,您应该首先修剪
查询

您可以。或者,您可以只使用
String.Split()
我不明白您的原始搜索数据会是什么样子。字面上是一个查询字符串,如
?something=value&anotherThing=other-value
?如果可以迭代查询字符串,为什么要解析它?也许我应该学习更多关于regex的知识,以便能够分析所有的案例。谢谢你的建议