正则表达式分组,多个具有分隔符的组

时间:2013-12-15 12:42:32

标签: regex vb.net

我再次练习正则表达式。这是我个人的学习。我使用的是Visual Basic,可以使用.NET方法轻松完成。

好的,这是一个示例字符串。

Dim example = "HelloWorld this! is! a! example!"

HelloWorld是第一场比赛。如果HelloWorld不存在,则总匹配应该失败。如果HelloWorld存在,我想要的组匹配是这个的四个匹配!是!一个!例!所以基本上我应该留下波纹管忽略了稀释剂!

Match 1) This Match 2) is Match 3) a Match 4) example 

HelloWorld ([a-z]+)!*

搜索堆栈溢出将我带到此链接,这只会匹配此Regex to find words that start with a specific character?但我不能修改它以满足我的需要。

Thansk寻求帮助

2 个答案:

答案 0 :(得分:0)

您可以使用群组的Capture属性

捕获群组中的多个值

在c#中就像是

string regex="HelloWorld( [a-z]+!)*";
Match match = Regex.Match(input, pattern);
if (match.Success) 
{
    foreach (Capture capture in match.Groups[1].Captures) 
    {
               Console.WriteLine(capture.Value.Trim(new char[]{'!',' '}));
    }
}

答案 1 :(得分:0)

参考Repeating a Capturing Group vs. Capturing a Repeated Group,不使用trim(.NET方法)的最终表达式:

 HelloWorld(( [a-z]+)!)*