c# - 正则表达式找不到所有匹配项

标签 c# regex string

已编辑: 我有一个 string str = "where dog is and cats are and bird is bigger than a mouse"并希望在 where 之间提取单独的子字符串和 and , andand , and和句子的结尾。结果应该是:dog is , cats are , bird is bigger than a mouse . (示例字符串可能包含 whereand 等之间的任何子字符串。)

List<string> list = new List<string>();
string sample = "where dog is and cats are and bird is bigger than a mouse";
MatchCollection matches = Regex.Matches(sample, @"where|and\s(?<gr>.+)and|$");
foreach (Match m in matches)
  {
     list.Add(m.Groups["gr"].Value.ToString());
  }

但它不起作用。我知道正则表达式不对,所以请帮我改正。谢谢。

最佳答案

“\w+ is”怎么样

  List<string> list = new List<string>();
string sample = "where dog is and cat is and bird is";
MatchCollection matches = Regex.Matches(sample, @"\w+ is");
foreach (Match m in matches)
{
    list.Add(m.Value.ToString());
}

样本:https://dotnetfiddle.net/pMMMrU

关于c# - 正则表达式找不到所有匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43590563/

相关文章:

c# - EmguCV 检测器与 EmguCV DescriptorExtractor 有何关系?

regex - 保持大小写与正则表达式查找和替换

Java正则表达式。获取某些关键字之间的子字符串

string - Scala中如何将字符串拆分为字符

c# - MVC : The parameters dictionary contains a null entry for parameter 'k' of non-nullable type 'System.Int32'

c# - ProgressHUD 和 TouchUpInside

java - 如何使用正则表达式检查字符串是否包含受限单词?

javascript - 如何从文本中删除字符串

c# - LINQ 中两个 "where"的区别

javascript - 将字符串与正则表达式匹配以获得 '=' 之间的子字符串?