RegEx在字符串中多次匹配

Mik*_*ell 24 c# regex

我正在尝试从字符串中提取<<和>>之间的值.但它们可能会发生多次.

任何人都可以帮助使用正则表达式来匹配这些;

this is a test for <<bob>> who like <<books>>
test 2 <<frank>> likes nothing
test 3 <<what>> <<on>> <<earth>> <<this>> <<is>> <<too>> <<much>>.
Run Code Online (Sandbox Code Playgroud)

然后我想要预先通过GroupCollection获取所有值.

任何帮助都很受欢迎.谢谢.

Pet*_*den 47

使用正向前看并查看断言以匹配尖括号,用于.*?匹配这些括号之间的最短可能字符序列.通过迭代方法MatchCollection返回的值来查找所有值Matches().

Regex regex = new Regex("(?<=<<).*?(?=>>)");

foreach (Match match in regex.Matches(
    "this is a test for <<bob>> who like <<books>>"))
{
    Console.WriteLine(match.Value);
}
Run Code Online (Sandbox Code Playgroud)

DotNetFiddle中的LiveDemo