RegEx在字符串中多次匹配

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

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

this is a test for <> who like <> test 2 <> likes nothing test 3 <> <> <> <> <> <> <>. 

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

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

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

 Regex regex = new Regex("(?<=<<).*?(?=>>)"); foreach (Match match in regex.Matches( "this is a test for <> who like <>")) { Console.WriteLine(match.Value); } 

您可以尝试以下方法之一:

 (?<=<<)[^>]+(?=>>) (?<=<<)\w+(?=>>) 

但是,您必须迭代返回的MatchCollection。

像这样的东西:

 (<<(?[^>]*)>>)* 

这个程序可能很有用:

http://sourceforge.net/projects/regulator/