Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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# 用于固定和#x27的正则表达式;和';和';或';用c实现的in-solr查询#_C#_Regex_Solr - Fatal编程技术网

C# 用于固定和#x27的正则表达式;和';和';或';用c实现的in-solr查询#

C# 用于固定和#x27的正则表达式;和';和';或';用c实现的in-solr查询#,c#,regex,solr,C#,Regex,Solr,我们需要通过c#dot net web应用程序处理和/或输入用户,以解决“q=”查询。它还必须正确处理引用的短语(这是最难的部分…) Da规则: 必须删除“或”,除非它位于带引号的字符串中。 “And”必须全部为大写,除非它位于带引号的字符串中 当然,问题是一个匹配或也匹配“OR”的正则表达式,我们需要一个匹配或但不匹配“OR”的正则表达式 给定输入: A或B、c或d、e或f 输出必须是: A B、c或d、e或f 给定输入: A和B,c和d,e和f 输出必须是: A和B、c和d、e和f解决方案:

我们需要通过c#dot net web应用程序处理和/或输入用户,以解决“q=”查询。它还必须正确处理引用的短语(这是最难的部分…)

Da规则: 必须删除“或”,除非它位于带引号的字符串中。 “And”必须全部为大写,除非它位于带引号的字符串中

当然,问题是一个匹配或也匹配“OR”的正则表达式,我们需要一个匹配或但不匹配“OR”的正则表达式

给定输入: A或B、c或d、e或f

输出必须是: A B、c或d、e或f

给定输入: A和B,c和d,e和f

输出必须是: A和B、c和d、e和f解决方案: 匹配OR和“OR”(OR和“and”)(假设快5倍),并使用自定义替换委托来确定我们是否要替换,是这样,替换什么

public string Fixup(string input)
{
//matches any quoted string containing the words OR or AND: "a and b" matches, 
//"andor" does not. 
string pattern1=@"""\w*?\W*?\b(AND|OR)\W*?\w*?"""; 
string pattern2=@"\b(AND|OR)\b"; //matches AND or OR as standalone words
string pattern3=pattern1+"|"+pattern2;//matches either pattern

MatchEvaluator Eval=ReplaceMatch;//set the delegate

string output=Regex.Replace(input,pattern3,Eval,RegexOptions.IgnoreCase);

return output;
}

public string ReplaceMatch(Match m)
{
string str=m.Value;
if(str.Contains("\""))return str;//do nothing if it's a quoted string
if(str.ToLower().Contains("or")) return String.Empty;//strip out 'or' from the query
return str.ToUpper();// string is 'and', uppercase it.
}
假设“不能在带引号的字符串中转义”,也可以在MatchEvaluator中使用组,如下所示:

// Check for "[^"]*" first to filter out any quoted strings
// Assign any matches of AND to the "AND" group
// Assign any matches of OR to the "OR" group
const string pattern = @"(""[^""]*"")|\s+((?<AND>AND)|(?<OR>OR))\s+";

public static string FixUp(string s)
{
    return Regex.Replace(s, pattern, ReplaceANDsAndORs, RegexOptions.IgnoreCase);
}

public static string ReplaceANDsAndORs(Match m)
{
    if (m.Groups["AND"].Length > 0)
    {
        return " AND ";
    }
    else if (m.Groups["OR"].Length > 0)
    {
        return " ";
    }
    else
    {
        return m.Value;
    }
}
//首先检查“[^”]*”以过滤掉任何带引号的字符串
//将和的任何匹配项分配给“和”组
//将或的任何匹配项分配给“或”组
常量字符串模式=@“(“[^”“]*”)|\s+((?和)|(?或))\s+”;
公共静态字符串修复(字符串s)
{
返回Regex.Replace(s,pattern,ReplaceANDsAndORs,RegexOptions.IgnoreCase);
}
公共静态字符串替换和删除(匹配m)
{
如果(m.组[“和”]。长度>0)
{
返回“和”;
}
如果(m.Groups[”或“]”长度>0,则为else
{
返回“”;
}
其他的
{
返回m.值;
}
}

更新:对“和”匹配项的处理正在删除它们周围的空白(即,“a和b”被更新为“aANDb”)。这已更正。

是否允许转义字符,或者“始终是带引号字符串的开头或结尾?带引号的字符串始终以开头和结尾”。例如:“This and that或“a and b”返回“This and that”和“a and b”任何其他lucene查询修饰符都要经过unmodified。regex并不是解决所有文本操作问题的方法。引用的报价呢?您是否考虑过使用.Net客户端进行Solr?SolrNet-