2012-12-06 92 views
1

我有一個文本塊:使用正則表達式在列表中搜索文本?

美國的我們人民,以形成一個更完美的 的聯邦,樹立正義,保障國內的安寧,爲 共同的國防,增進一般福利,並確保自己和我們的後代的自由的祝福,做的和 爲美利堅合衆國制定本憲法。

然後我有一個包含列表的幾個關鍵詞:

List<string> keywords = new List<string>() 
{ 
    "Posterity", 
    "Liberty", 
    "Order", 
    "Dinosaurs" 
} 

這是我期望的使用:

List<string> GetOrderOfOccurence(string text, List<string> keywords); 

所以調用GetOrderOfOccurence(序言,關鍵字)將返回以下順序:

{"Order"}, 
{"Liberty"}, 
{"Posterity"} 

This can eas可以用關鍵字上的for循環和前導碼上的getIndexOf(關鍵字)來解決;然後將索引推入列表並返回該列表。這將如何使用Regex完成?假設我想在我的關鍵字列表中使用通配符?

System.Text.RegularExpressions.Regex.Matches()是否有一些使用模式列表的東西?

回答

0

您可以使用它包含keywords通過管道分隔的字符串的集合單組|作爲一種模式,如果你想使用正則表達式匹配字符串。然後,搜索字符串text,這種模式將它們添加到一個新的List<string>這將在隨後返回GetOrderOfOccurence(string text, List<string> keywords)

List<string> GetOrderOfOccurence(string text, List<string> keywords) 
{ 
    List<string> target = new List<string>(); //Initialize a new List of string array of name target 
    #region Creating the pattern 
    string Pattern = "("; //Initialize a new string of name Pattern as "(" 
    foreach (string x in keywords) //Get x as a string for every string in keywords 
    { 
     Pattern += x + "|"; //Append the string + "|" to Pattern 
    } 
    Pattern = Pattern.Remove(Pattern.LastIndexOf('|')); //Remove the last pipeline character from Pattern 
    Pattern += ")"; //Append ")" to the Pattern 
    #endregion 
    Regex _Regex = new Regex(Pattern); //Initialize a new class of Regex as _Regex 
    foreach (Match item in _Regex.Matches(text)) //Get item as a Match for every Match in _Regex.Matches(text) 
    { 
     target.Add(item.Value); //Add the value of the item to the list we are going to return 
    } 
    return target; //Return the list 
} 
private void Form1_Load(object sender, EventArgs e) 
{ 
    List<string> keywords = new List<string>(){"Posterity", "Liberty", "Order", "Dinosaurs"}; //Initialize a new List<string> of name keywords which contains 4 items 
    foreach (string x in GetOrderOfOccurence("We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.", keywords)) //Get x for every string in the List<string> returned by GetOrderOfOccurence(string text, List<string> keywords) 
    { 
     Debug.WriteLine(x); //Writes the string in the output Window 
    } 
} 

輸出

Order 
Liberty 
Posterity 

由於相匹配,
我希望你覺得這有幫助:)

3

你必須使用正則表達式嗎? Linq可以做到這一點。

例子:

private List<string> GetOrderOfOccurence(string text, List<string> keywords) 
{ 
    return keywords.Where(x => text.Contains(x)).OrderBy(x => text.IndexOf(x)).ToList(); 
} 

返回

{"Order"}, 
{"Liberty"}, 
{"Posterity"}