2014-11-21 1 views
0

검색을 위해 input string이라는 정적 메소드가 있습니다. 이 방법에서는이 입력 문자열을 공백에서 분리하고 각각에 검색 알고리즘 (RavenQueryable)을 사용합니다. 이 검색 입력에는 (네덜란드어) 우편 번호가 포함될 수 있으며 고객은 공백이 있는지 여부와 관련하여 모든 검색을 원합니다. 반 코드에서검색 : 정규식과 일치하는 경우 추가 (공백 또는 공백없이)

- 내가 가진 무엇 : 사용자가 '또는 공백 아무튼없이 (A 우편 번호를 입력 할 때

// 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 

이 방법을 : 나는 그것을 대체 할 무엇

// 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 

을 모든 문제 (공백의 유무와 상관없이)를 모두 찾습니다.

예 :

  • 사용자는 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 번째 문자 다음에 공백을 추가/바꾸기 위해 문자열 하위 문자열을 사용합니다. 하지만 사용자가 올바른 검색 (예 : 3434A/34A, 1234AB 및 1234AB를 검색해야 함)으로 우편 번호의 일부를 넣을 수있게하려는 경우에도 하위 문자열을 사용할 수 없습니다. 네 번째 문자.

나는 이것이 내가 달성하기를 원하는 것의 일부와 내가 갇혀있는 곳을 깨끗이 해주기를 바란다. regex에 대치 정규 표현식 대신 공백 문자 (공백과 같은 공백 문자)를 사용하는 것이 좋습니다.


편집 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); 
+1

당신이 이미 가지고있는 것을 보여주고있는 것이 좋지만 그 질문은 무엇입니까? – khellang

+0

@khellang 수정되었습니다. 공백이있는 경우와없는 경우 모두 일치하는 우편 번호를 검색하는 코드를 알고 싶습니다. –

+0

이것은 실제로 "나를 위해 codez을 작성하십시오"유형의 장소가 아닙니다. 의사 코드를 제어 할 수있는 것 같습니다. _real_ 문제가 생겼을 때 그것을 쓰고 다시 돌아와보십시오. ;) – khellang

답변

0

좋아, 몇 가지 나는 아래이 반 답을 건너 온 :

  • 나는 .Split(' ');을 할 수 있기 때문에, 난 그냥 필요가 공백으로 이미 사람을 평가 할 필요가 없습니다 정확한 공백으로 우편 번호를 추가합니다 (공백 대신).
  • 분명히 변수 이름을 정규 표현식 부분에 할당 할 수 있고 Regex.Replace을 사용할 수 있습니다. > "1234AB"
  • -

    • "1234AB"

      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); 
      

      몇 가지 예 :

    그래서 지금은 내 정규식 문제 대체 수정 나의 방법에 다음 코드를

  • "일부 단어 1234AB"-> "일부 단어 1234AB"
  • "1234 AB 1234AB"
  • "some words 1234 AB"-> "some words 1234 AB 1234AB"

이렇게하면 내 정규식이 실제로 수정됩니다. 유일한 문제는 RavenQueryable#SearchMultipleSearchOptions.And을 사용하므로 더 이상 일치하지 않습니다.

기본적으로 위의 코드는 my regex 바꾸기 문제를 해결하지만 이제 문자열 배열의 일부를 SearcOptions.Or (공백을 포함하거나 포함하지 않는 포스트 코드)으로 사용하는 방법을 알아야하고 나머지는 우편 번호)는 SearchOptions.And입니다. 이것은 완전히 새로운 문제입니다. 먼저 제 동료 인 나와 제게 Raven에 대해 많이 알고 있습니다. 그리고 그가 해결책을 모른다면 나는 새로운 질문을 할 것입니다.


편집 : 우리는 우리가 모든 것을 가져올 때 우리가 새로 저장할 때없이 동일한 부분에 공백이있는 모든 우편 번호를 변환하기로 결정했습니다. 그래서 위의 정규 표현식을 가져 오기 부분에 적용한 후에는 사용자가 추가 된 새 코드에 대해 공백없이 포스트 코드 만 입력 할 수 있습니다.