Skip to content Skip to sidebar Skip to footer

Find The Common Occurrences Of Words In Two Strings Regex

I'm trying to do the same thing as: Find The Common Occurrences Of Words In Two Strings But I want it in VB.net. var tester = 'a string with a lot of words'; function getMeRepeate

Solution 1:

I think I figured it out:

    'string of interest
    Dim sentence As String = "check this string with some words"
    sentence = sentence & " "

    'this is our base string
    Dim BaseStr As String = "base string with a lot of words"

    'use this one liner to create the pattern
    Dim pattern2 As String = "(" & BaseStr.Replace(" ", "|") & ")\W"

    'set up the regex
    Dim regex As New Regex(pattern2)
    Dim mc As MatchCollection
    mc = regex.Matches(sentence)

    'loop to get the results
    For ct As Integer = 0 To mc.Count - 1
        Dim m As Match = mc.Item(ct)
        If m.Success Then
            'returns two groups; get the second without a space
            Debug.Print(m.Groups(1).Captures(0).ToString() & "<<")
        End If
    Next

Post a Comment for "Find The Common Occurrences Of Words In Two Strings Regex"