2013-01-15 31 views
1

比如我有一個輸入:(重複兩次)"Test your Internet connection bandwidth. Test your Internet connection bandwidth.",我想搜索字符串互聯網帶寬查找使用正則表達式兩個字符串以任意順序

string keyword = tbSearch.Text //That holds value: "internet bandwidth" 
string input = "Test your Internet connection bandwidth. Test your Internet connection bandwidth."; 

Regex r = new Regex(keyword.Replace(' ', '|'), RegexOptions.IgnoreCase); 
if (r.Matches(input).Count == siteKeyword.Split(' ').Length) 
{ 
    //Do something 
} 

這不工作的原因找到2「互聯網」和2個「帶寬」,所以算4,但關鍵詞長度爲2。所以,我能做些什麼?

+0

如果你只是想搜索「互聯網」和「帶寬」你爲什麼要使用這個複雜的代碼?你還想做更多的事嗎? – Schaliasos

+0

我剛在這個例子中使用了2個關鍵字,他可以給更多 – a1204773

+0

你想做什麼?檢查輸入字符串中的所有關鍵字? –

回答

4
var pattern = keyword.Split() 
     .Aggregate(new StringBuilder(), 
        (sb, s) => sb.AppendFormat(@"(?=.*\b{0}\b)", Regex.Escape(s)), 
        sb => sb.ToString()); 

if (Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase)) 
{ 
    // contains all keywords 
} 

第一部分是從您的關鍵字生成模式。如果有兩個關鍵字"internet bandwidth",然後生成的正則表達式模式將類似於:

"(?=.*\binternet\b)(?=.*\bbandwidth\b)"

它將匹配下列輸入:

"Test your Internet connection bandwidth." 
"Test your Internet connection bandwidth. Test your Internet bandwidth." 

以下投入將不匹配(不是全部的話包含):

"Test your Internet2 connection bandwidth bandwidth." 
"Test your connection bandwidth." 

另一個OPTIO N(分別驗證每個關鍵字):

var allWordsContained = keyword.Split().All(word => 
    Regex.IsMatch(input, String.Format(@"\b{0}\b", Regex.Escape(word)), RegexOptions.IgnoreCase)); 
+0

nice1但是'。*'應該是'。*?',因爲他要計算匹配的數量。 – Anirudha

+0

@ Some1.Kill.The.DJ實際上他想知道包含在字符串中的所有單詞'只是他試圖這樣做的一種方式),所以我認爲它會這樣做:) –

+0

ohh..my壞..... – Anirudha

0

不知道你正在嘗試做的,但你可以嘗試這樣的:

public bool allWordsContained(string input, string keyword) 
{ 
    bool result = true; 
    string[] words = keyword.Split(' '); 

    foreach (var word in words) 
    { 
     if (!input.Contains(word)) 
      result = false; 
    } 

    return result; 
} 

public bool atLeastOneWordContained(string input, string keyword) 
{ 
    bool result = false; 
    string[] words = keyword.Split(' '); 

    foreach (var word in words) 
    { 
     if (input.Contains(word)) 
      result = true; 
    } 

    return result; 
} 
0

這裏是解決方案。線索是獲得結果的名單,並鮮明的()...

string keyword = "internet bandwidth"; 
    string input = "Test your Internet connection bandwidth. Test your Internet connection bandwidth."; 

    Regex r = new Regex(keyword.Replace(' ', '|'), RegexOptions.IgnoreCase); 
    MatchCollection mc = r.Matches(input); 
    List<string> res = new List<string>(); 

    for (int i = 0; i < mc.Count;i++) 
    { 
     res.Add(mc[i].Value); 
    } 

    if (res.Distinct().Count() == keyword.Split(' ').Length) 
    { 
     //Do something 
    } 
0
Regex r = new Regex(keyword.Replace(' ', '|'), RegexOptions.IgnoreCase); 
int distinctKeywordsFound = r.Matches(input) 
          .Cast<Match>() 
          .Select(m => m.Value) 
          .Distinct() 
          .Count(); 
if (distinctKeywordsFound == siteKeyword.Split(' ').Length) 
{ 
    //Do something 
} 
相關問題