我有像这样的一些搜索查询:
乔治和NOT华盛顿和亚伯拉罕
狗或猫,不是狼
针对这些搜索我要回去了的乔治或亚伯拉罕结果但华盛顿的等。
基本上我想借此字符串,并可以提交一个上下文搜索到我的全文目录存储过程的搜索。
我假设我应该使用正则表达式,但我很熟悉正则表达式在C#。
我发现这文章: support.microsoft/kb/246800 我认为这是我。需要做的,但我希望我能有一些帮助,实施
假设你把一个字符串作为参数,并想返回一个字符串:
字符串输入='乔治·华盛顿AND NOT玛莎或狗'; 私人字符串interpretSearchQuery(输入) { // HALP! / *替换和| 而不是与 *和 *而不是 * *代替'或'| OR NOT与 *或 *OR NOT * *添加到字符串的开头和结束串 *的/ 返回'乔治·华盛顿,而不是玛莎或狗'; }解决方案
我会使用解析您的字符串后缀表示法(或波兰式)。
** Postfix的算法** 中的算法评估任何后缀表达式是相当简单: 虽然有输入记号留下 读取输入的下一个标记。 。如果标记是一个价值其推入堆栈。 否则,该标记是一个运营商(运营商这里既包括运算符和函数)。 已知的先验,操作者需要n个参数。 。如果有对堆栈(错误)用户大于n的值少具有在表达式未输入足够的值。 ,否则,从栈中弹出的前n值。 评估运营商,拥有该值作为参数。 按下返回的结果,如果有的话,背面压入堆栈。 。如果只有一个在堆栈该值的值是计算的结果。 。如果有在堆栈(错误)的用户输入有值过多多个值。
所以,把你的输入字符串:
乔治·华盛顿和不玛莎或狗
和它simplifing为:
A =乔治 b =华盛顿 C =玛莎 D =狗 &安培; = AND ! = NOT | = OR我们将获得的
后缀符号!AB和C开发|
这意味着:
I have some search queries like so:
George AND NOT Washington OR Abraham
Dog OR cat AND NOT Wolf
for these searches I would want to get back results for George or Abraham but not Washington, etc.
basically I want to take the string and be able to submit a contextual search to my full-text catalog stored procedure search.
I am assuming I should use Regex but I am very unfamiliar with Regex in C#.
I found this article: support.microsoft/kb/246800 which I think is what I need to do, but I was hoping that I could have some help with the implementation.
Assuming you take a string as parameter and would like to return a string:
string input = 'George Washington AND NOT Martha OR Dog'; private string interpretSearchQuery(input) { // HALP! /* replace ' AND ' | ' AND NOT ' with * " AND " * " AND NOT " * * replace ' OR ' | ' OR NOT ' with * " OR " * " OR NOT " * * add " to beginning of string and " to end of string */ return '"George Washington" AND NOT "Martha" OR "Dog"'; }解决方案
I would parse your string using Postfix notation (or Polish notation).
**Postfix algorithm** The algorithm for evaluating any postfix expression is fairly straightforward: While there are input tokens left Read the next token from input. If the token is a value Push it onto the stack. Otherwise, the token is an operator (operator here includes both operators, and functions). It is known a priori that the operator takes n arguments. If there are fewer than n values on the stack (Error) The user has not input sufficient values in the expression. Else, Pop the top n values from the stack. Evaluate the operator, with the values as arguments. Push the returned results, if any, back onto the stack. If there is only one value in the stack That value is the result of the calculation. If there are more values in the stack (Error) The user input has too many values.So taking your input string:
'George Washington AND NOT Martha OR Dog'
And simplifing it to:
A = George B = Washington C = Martha D = Dog & = AND ! = NOT | = ORWe would get the postfix notation of
AB&C!D|
Which means:
更多推荐
HOWTO:C#字符串到SQL全文目录搜索?
发布评论