已编辑:
我有一个 string str = "where dog is and cats are and bird is bigger than a mouse"
并希望在 where
之间提取单独的子字符串和 and
, and
和 and
, and
和句子的结尾。结果应该是:dog is
, cats are
, bird is bigger than a mouse
. (示例字符串可能包含 where
和 and
等之间的任何子字符串。)
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());
}
关于c# - 正则表达式找不到所有匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43590563/