2013-09-30 2 views
1

Entity Framework에서 키워드 형식으로 검색 기능을 작성하려고합니다.검색 쿼리 문자열을 구문 분석하는 방법 SO

void funcSearch(string keywork) 
     { 
      if (keywork == "[tag]") 
      { 
       //regex for is tag 
       //do search tag 
      } 
      if (keywork == "user:1234") 
      { 
       //regex for userid is 1234 
       //do search user with 1234 
      } 
      ... 
     } 

정규식을 사용하여 SO 또는 다른 방법과 같은 쿼리 문자열 형식을 구문 분석 할 수 있습니까? 해당 키워드로 모든 사례를 분석 할 수있는 기능?

tags [tag] 
exact "words here" 
author user:1234 
user:me (yours) 
score score:3 (3+) 
score:0 (none) 
answers answers:3 (3+) 
answers:0 (none) 
isaccepted:yes 
hasaccepted:no 
inquestion:1234 
views views:250 
sections title:apples 
body:"apples oranges" 
url url:"*.example.com" 
favorites infavorites:mine 
infavorites:1234 
status closed:yes 
duplicate:no 
migrated:no 
wiki:no 
types is:question 
is:answer 

감사합니다.

+1

수 있습니다. 아니면 그냥 String.Split()을 쓸 수 있습니다. –

+0

원시 검색 데이터가 어떻게 보이는지 알 수 없습니다. 문자 그대로 '? something = value & anotherThing = another Value'와 같은 질의 문자열? – Yuck

+0

왜 반복 할 수 있는지 querystring을 구문 분석합니까? http://msdn.microsoft.com/de-de/library/system.web.httprequest.querystring.aspx – DrCopyPaste

답변

3

예, 가능합니다. 일치하는 것을 찾을 때까지 정규 표현식의 목록을 확인하고 반복해야합니다. (정확하게 우선 순위를해야합니다.) 예를 들어

, 검색 쿼리 태그를 쿼리되어 있는지 확인하려면, 다음과 같은 정규 표현식 사용할 수 있습니다 여기에

string query = "[tag]"; 
bool isTag = Regex.IsMatch(query, @"^\[.+?\]$"); 

를 다른 정규식 사용자 ID와 일치하는 것 :

먼저 query을 잘라야합니다.

+1

아마도 모든 사례를 분석 할 수 있도록 정규식에 대해 더 자세히 알아야합니다. 조언에 감사드립니다. –

관련 문제