Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用正则表达式根据列表搜索文本?_C#_Regex_C# 4.0 - Fatal编程技术网

C# 使用正则表达式根据列表搜索文本?

C# 使用正则表达式根据列表搜索文本?,c#,regex,c#-4.0,C#,Regex,C# 4.0,我有一段文字: 我们美国人民,为了形成一个更加完美的世界 团结,建立正义,确保家庭安宁,提供 共同防御,促进普遍福利,确保 自由的祝福带给我们自己和我们的子孙后代,一定要注定 为美利坚合众国制定本宪法 然后我有一个包含几个关键词的列表: List<string> keywords = new List<string>() { "Posterity", "Liberty", "Order", "Dinosaurs" } 这可以通过关键字上的for循环和前导

我有一段文字:

我们美国人民,为了形成一个更加完美的世界 团结,建立正义,确保家庭安宁,提供 共同防御,促进普遍福利,确保 自由的祝福带给我们自己和我们的子孙后代,一定要注定 为美利坚合众国制定本宪法

然后我有一个包含几个关键词的列表:

List<string> keywords = new List<string>()
{
  "Posterity",
  "Liberty",
  "Order",
  "Dinosaurs"
}
这可以通过关键字上的for循环和前导码上的getIndexOf(关键字)轻松解决;然后将索引放入列表并返回该列表。如何使用Regex实现这一点?假设我想在关键字列表中使用通配符


System.Text.RegularExpressions.Regex.Matches()是否具有使用模式列表的功能?

是否必须使用Regex?林克可能会做得很好

例如:

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

如果要使用正则表达式匹配字符串,可以使用包含由管道分隔的
关键字
字符串集合的单个组作为模式。然后,在
text
中搜索与此模式匹配的字符串,并将其添加到新的
列表
,然后将其作为
GetOrderOfOccurence(字符串文本,列表关键字)返回。

示例

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
    }
}
谢谢,
我希望这对您有所帮助:)

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"}
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
    }
}