1

私はテキストのブロックを持っています:

私たち合衆国国民は、より完全な連邦を形成し、正義を確立し、国内の平和を保証し、共通の防衛を提供し、一般的な福祉を促進し、自由の祝福を私たち自身と私たちの子孫に確保するために、次のことを行います。アメリカ合衆国のためにこの憲法を制定する。

次に、いくつかのキーワードを含むリストを作成します。

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

これが私の希望する使用法です:

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

そのため、GetOrderOfOccurence(プリアンブル、キーワード) を呼び出すと、次のものが順番に返されます。

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

これは、キーワードの for ループとプリアンブルの getIndexOf(keyword) で簡単に解決できます。次に、インデックスをリストにプッシュして返します。これは正規表現を使用してどのように行われますか? キーワード リストにワイルドカードを使用したいとします。

System.Text.RegularExpressions.Regex.Matches() には、パターンのリストを使用するものがありますか?

4

2 に答える 2

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"}
于 2012-12-06T04:14:12.953 に答える
0

正規表現を使用して文字列を照合する場合はkeywords、パイプラインで区切られた文字列のコレクションを含む単一のグループをパターンとして使用できます。次に、このパターンに一致|する文字列を検索し、それらを new に追加します。これは次のように返されますtextList<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

ありがとう、
これがお役に立てば幸いです:)

于 2012-12-06T04:42:23.277 に答える