C# 搜索:匹配正则表达式时添加(带或不带空格)

C# 搜索:匹配正则表达式时添加(带或不带空格),c#,regex,wpf,search,whitespace,C#,Regex,Wpf,Search,Whitespace,我有一个静态方法,用于获取搜索的输入字符串。在该方法中,它在空间上拆分该输入字符串,并对每个字符串使用搜索算法(RavenQueryable)。此搜索输入可以包括(荷兰)邮政编码,客户希望搜索所有邮政编码,以确定是否有空间 在半代码中-我所拥有的: // Replace multiple whitespaces in the search-input for a single one // Split the search-input at a single space // Use Raven

我有一个静态方法,用于获取搜索的
输入字符串。在该方法中,它在空间上拆分该输入字符串,并对每个字符串使用搜索算法(RavenQueryable)。此搜索输入可以包括(荷兰)邮政编码,客户希望搜索所有邮政编码,以确定是否有空间

在半代码中-我所拥有的:

// Replace multiple whitespaces in the search-input for a single one
// Split the search-input at a single space
// Use RavenQueryable's SearchMultiple-method on this array of strings
我想用以下内容替换它:

// Replace multiple whitespaces in the search-input for a single one
// Find a (part of) a postcode regex with a whitespace "[1-9][0-9]{3}[ ][A-Za-z]{2}" or @"[\d][ ][A-Za-z]"
// var string with this postcode without spaces (replaced for "[1-9][0-9]{3}[A-Za-z]{2}" or @"[\d][A-Za-z]")
// Find a postcode regex without a whitespace "[1-9][0-9]{3}[A-Za-z]{2}" or @"[\d][A-Za-z]"
// var string with this postcode with a single whitespace (replaced for "[1-9][0-9]{3}[ ][A-Za-z]{2}" or @"[\d][ ][A-Za-z]")
// Split the search-input at a single space
// Use RavenQueryable's SearchMultiple-method on this array of strings
这样,当用户输入一个邮政编码(带或不带空格都无所谓)时,它将查找所有发生的事件(带或不带空格)

例如:

  • 当用户输入1234 AB时:它给出带有1234 AB和1234 AB的两个项目的结果
  • 当用户输入1234AB时:它给出带有1234AB和1234 AB的两个项目的结果
我已经有了一些代码:

public static IRavenQueryable<T> SearchMultiple<T>(this IRavenQueryable<T> self,
    Expression<Func<T, object>> fieldSelector, string queries,
    decimal boost = 1, SearchOptions options = SearchOptions.Or)
{
    if(string.IsNullOrEmpty(queries) throw new ArgumentNullException("queries");

    queries = Regex.Replace(queries, @"\s{2,}", " ");
    // Postcode code
    var searchValues = queries.Split(' ');

    return self.SearchMultiple(fieldSelector, searchValues, boost, options);
}

好的,我在做下面这个半答案时遇到了一些事情:

  • 因为我做了一个
    .Split(“”)
    ,我不需要计算已经有空格的邮政编码,我只需要将没有空格的邮政编码添加到查询中,作为完全相同的邮政编码(但使用空格)
  • 显然,您可以为regex部件指定变量名,然后使用
    regex.Replace
因此,现在我的方法中有以下代码,它修复了我的正则表达式替换问题:

if (string.IsNullOrEmpty(queries)) throw new ArgumentNullException("queries");

var newQueries = Regex.Replace(queries, @"\s{2,}", " ");
var withSpaceRegex = @"(?<decimals>[0-9]+)[ ](?<letters>[A-Za-z]+)";
var replacementWithSpace = "${decimals}${letters}";
var postcodesWithSpace = Regex.Matches(newQueries, withSpaceRegex);
newQueries = postcodesWithSpace.Cast<object>().Aggregate(newQueries, (current, s) => current
    + " " + Regex.Replace(s.ToString(), withSpaceRegex, replacementWithSpace, RegexOptions.IgnoreCase));
var searchValues = newQueries.Split(' ');

return self.SearchMultiple(fieldSelector, searchValues, boost, options);
if(string.IsNullOrEmpty(查询))抛出新的ArgumentNullException(“查询”);
var newquerys=Regex.Replace(查询@“\s{2,}”和“”);
var with spaceregex=@“(?[0-9]+)[](?[A-Za-z]+)”;
var replacementWithSpace=“${decimals}${letters}”;
var postcodesWithSpace=Regex.Matches(newquerys,withSpaceRegex);
newquerys=postcodesWithSpace.Cast().Aggregate(newquerys,(current,s)=>current
+“”+Regex.Replace(s.ToString(),with spaceregex,replacementWithSpace,RegexOptions.IgnoreCase));
var searchValues=newquerys.Split(“”);
返回self.SearchMultiple(字段选择器、searchValues、boost、选项);
一些例子:

  • “1234AB”->“1234AB”
  • “一些单词1234AB”->“一些单词1234AB”
  • “1234 AB”->“1234 AB 1234AB”
  • “一些单词1234 AB”->“一些单词1234 AB 1234AB”
所以这确实修复了我的正则表达式。唯一的问题是我们使用了一个
SearchOptions.And
作为
RavenQueryable#SearchMultiple
,所以现在它也不再匹配了

基本上,上面的代码修复了我的正则表达式替换问题,但现在我需要弄清楚如何将字符串数组的一部分用作
searcopions.或
(带空格和不带空格的邮政编码),将其余部分(包括这些邮政编码Or)用作
SearchOptions.和
。然而,这是一个全新的问题,我将首先与一位比我更了解
Raven
的同事讨论,如果他也不知道解决方案,我将提出一个新问题



编辑:我们决定在导入所有内容和保存新内容时,将所有带空格的邮政编码转换为相等的部分。因此,我刚刚将上面的正则表达式应用于导入部分,之后只允许用户输入邮政编码,而无需为添加的新邮政编码留出空间。

很好,您展示了您已经拥有的邮政编码,但问题是什么?@khellang编辑。我想知道搜索带有空格和不带空格的邮政编码的代码这不是一个真正的“请为我写代码”类型的地方。看起来你已经控制了伪代码,为什么不试着把它写出来,当你遇到真正的问题时再回来呢?;)@khellang再次编辑。我知道如何获取邮政编码部分,但我不知道如何将其替换为对应的部分(有空格到没有空格,反之亦然)。
if (string.IsNullOrEmpty(queries)) throw new ArgumentNullException("queries");

var newQueries = Regex.Replace(queries, @"\s{2,}", " ");
var withSpaceRegex = @"(?<decimals>[0-9]+)[ ](?<letters>[A-Za-z]+)";
var replacementWithSpace = "${decimals}${letters}";
var postcodesWithSpace = Regex.Matches(newQueries, withSpaceRegex);
newQueries = postcodesWithSpace.Cast<object>().Aggregate(newQueries, (current, s) => current
    + " " + Regex.Replace(s.ToString(), withSpaceRegex, replacementWithSpace, RegexOptions.IgnoreCase));
var searchValues = newQueries.Split(' ');

return self.SearchMultiple(fieldSelector, searchValues, boost, options);