Imports System.Text.RegularExpressions Module PermutationSearch Public Sub Main() Dim input As String = "abcd" Dim length As Integer = 3 Dim pattern As String = "*ab*" ' This could be any pattern Dim permutations As New List(Of String)() GetPermutations("", input, length, permutations) ' Filter and output results based on user-defined pattern Dim regexPattern As String = WildcardToRegex(pattern) Dim result As New List(Of String) For Each s In permutations If Regex.IsMatch(s, regexPattern) Then result.Add(s) Debug.WriteLine(s) End If Next End Sub Private Sub GetPermutations(solution As String, possibleChars As String, Optional minStrLen As Integer = 1, Optional ByRef solutions As List(Of String) = Nothing) If solutions Is Nothing Then solutions = New List(Of String)() End If If solution.Length >= minStrLen Then solutions.Add(solution) End If For i As Integer = 0 To possibleChars.Length - 1 Dim newSolution As String = solution & possibleChars(i) Dim newPossibleChars As String = possibleChars.Remove(i, 1) ' Remove the used character GetPermutations(newSolution, newPossibleChars, minStrLen, solutions) Next End Sub Private Function WildcardToRegex(pattern As String) As String ' Escape regex special characters and replace wildcard characters Dim regexPattern As String = Regex.Escape(pattern) regexPattern = regexPattern.Replace("\*", ".*") ' Replace * with .* regexPattern = regexPattern.Replace("\?", ".") ' Replace ? with . Return "^" & regexPattern & "$" ' Ensure that the pattern matches the entire string End Function End Module ''abc ''abcd ''abd ''abdc ''cab ''cabd ''cdab ''dab ''dabc ''dcab