【发布时间】:2014-11-21 09:24:34
【问题描述】:
我有一个静态方法可以获取input string 进行搜索。在这个方法中,它在空间分割这个输入字符串,并在每个输入字符串上使用搜索算法(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 时:它会给出 1234AB 和 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);
}
那么,我该如何制作这个 // Postcode code,以便我将“我拥有的半代码”替换为“我想用半代码替换它的内容”?
编辑:
- 我知道如何获取邮政编码正则表达式:
var postcode = Regex.Match(queries, "[1-9][0-9]{3}[A-Za-z]{2}"); - 我只是不知道如何用另一个正则表达式替换一个正则表达式。我知道有一个 Regex.Replace,但这会替换所选字符串的整个正则表达式。相反,我想要的是替换与正则表达式匹配的整个字符串,用于相同的字符串(但带有空格)。
如果我只接受整个邮政编码(如 1234AB / 1234 AB),我只会使用字符串子字符串在第 4 个字符后添加/替换空格。但是由于我还希望允许用户将部分邮政编码作为有效搜索(如 34A / 34 A,它们也需要搜索 1234AB 和 1234 AB),所以我不能在后面使用子字符串第 4 个字符。
我希望这可以清除一些我想要实现的目标以及我遇到的问题。是否有某种替换正则表达式的正则表达式加上添加字符(在我的情况下是空格)方法,因为那会很棒。
编辑 2:
好的,我找到了a regex for regex replace method here,我只是不知道如何将它应用到我的案例中。
当我尝试以下代码时,它会给出一个 ArgumentException,表明我的正则表达式不正确。我几乎从不使用 Regex,也不太了解它,因此我们将不胜感激。
if (string.IsNullOrEmpty(queries)) throw new ArgumentNullException("queries");
queries = Regex.Replace(queries, @"\s{2,}", " ");
const string withSpaceRegex = @"?<decimals>[\d][ ]?<letters>[A-Za-z]";
const string withoutSpaceRegex = @"?<decimals>[\d]?<letters>[A-Za-z]";
const string replacementWithSpace = "${decimals}${letters}";
const string replacementWithoutSpace = "${decimals} ${letters}";
var postcodesWithSpace = Regex.Matches(queries, withSpaceRegex);
var postcodesWithoutSpace = Regex.Matches(queries, withoutSpaceRegex);
queries = postcodesWithSpace.Cast<string>().Aggregate(queries, (current, s) => current
+ " " + Regex.Replace(s, s, replacementWithSpace, RegexOptions.IgnoreCase));
queries = postcodesWithoutSpace.Cast<string>().Aggregate(queries, (current, s) => current
+ " " + Regex.Replace(s, s, replacementWithoutSpace, RegexOptions.IgnoreCase));
var searchValues = queries.Split(' ');ostcodeWithoutSpace, RegexOptions.IgnoreCase));
var searchValues = queries.Split(' ');
return self.SearchMultiple(fieldSelector, searchValues, boost, options);
【问题讨论】:
-
很高兴你展示了你已经拥有的东西,但问题是什么?
-
@khillang 已编辑。我想知道搜索带有和不带有空格的邮政编码的代码
-
这不是一个真正的“请为我编写代码”类型的地方。看来您已经控制了伪代码,为什么不尝试将其写出来,然后在遇到真正的问题时再回来? ;)
-
@khillang 再次编辑。我知道如何获取邮政编码部分,只是不知道如何将其替换为对应部分(有空格到没有空格,反之亦然)。
标签: c# regex wpf search whitespace