Mega Code Archive

 
Categories / VB.Net / Development
 

Define a regular expression for repeated words

Imports System Imports System.Text.RegularExpressions Public Module Test     Public Sub Main()         Dim rx As New Regex("\b(?<word>\w+)\s+(\k<word>)\b",RegexOptions.Compiled Or RegexOptions.IgnoreCase)         Dim text As String = "this is is a test test."         Dim matches As MatchCollection = rx.Matches(text)         Console.WriteLine("{0} matches found in:", matches.Count)         For Each match As Match In matches             Dim groups As GroupCollection = match.Groups             Console.WriteLine("'{0}' repeated at positions {1} and {2}", _                               groups.Item("word").Value, _                               groups.Item(0).Index, _                               groups.Item(1).Index)         Next     End Sub End Module