Wildcard Replace using the VB.NET Replace function

I was hoping that one of the new features of .NET 2005 would include the ability to do wildcard replacing using the Replace() function. Is it possible or do i need to find a function or module Something like Replace(str1,"Test*","Test") is what im after. Is there something out there for it

Cheers


Answer this question

Wildcard Replace using the VB.NET Replace function

  • S Hussain

    what you have there is basically a wildcard search followed by a replace.

    The instr function will also be helpful.

    I wrote a parser a long time ago. I don't know whether this will help or not.

    Private Sub Parse()

            Dim i As Integer
            Dim illegalchars = "\/<>:;""'|^ "
            bListAllFiles = False
            bFullnameCompare = False
            bExtCompare = False
            bNameCompare = False
            bNamescan = False
            bExtscan = False
            bRequiredActions = 0

            sFilestring = tb1.Text.Trim
            Dim ISlm1 As Short = sFilestring.Length - 1

            'Scan for illegal characters

            If sFilestring <> "" Then
                For ii As Short = 0 To ISlm1
                    For i = 0 To illegalchars.length - 1
                        If sFilestring(ii) = illegalchars(i) Then
                            Dim status As Integer = MsgBox("'" & sFilestring(ii) & "' is an illegal filename character." & vbCrLf & "Do you want to continue ", MsgBoxStyle.YesNo, _
                            "Illegal Character")
                            If status = Microsoft.VisualBasic.MsgBoxResult.Yes Then
                                Exit For
                            Else
                                GoTo Common_Exit
                            End If

                        End If
                    Next
                Next
            Else
                bListAllFiles = True
                bRequiredActions += 1
                GoTo Common_Exit
            End If


            ' One contiguous asterisk please

            While sFilestring.Contains("**")
                sFilestring = sFilestring.Replace("**", "*")
            End While


            If ((sFilestring = "") Or (sFilestring = "*") Or sFilestring = ("*.*") Or sFilestring = ("*.") _
            Or sFilestring = (".*")) Then
                bListAllFiles = True
                bRequiredActions += 1
                GoTo Common_Exit
            End If

            Dim StarCount As Short
            Dim DotCount As Short

            ISlm1 = sFilestring.Length - 1 ' Recalculate

            For i = 0 To ISlm1
                If sFilestring(i) = "*" Then StarCount += 1
                If sFilestring(i) = "." Then DotCount += 1
            Next


            If (((StarCount = 0) And (DotCount = 0)) And ISlm1 > 0) Then
                bFullnameCompare = True
                bRequiredActions += 1
                GoTo Common_Exit
            End If


            Dim name As String = sFilestring.Substring(0, sFilestring.LastIndexOf("."))
            Dim ext As String = sFilestring.Substring(sFilestring.LastIndexOf(".") + 1, ISlm1 - sFilestring.LastIndexOf("."))

            If (Not name.Contains("*")) And (Not ext.Contains("*")) Then
                bFullnameCompare = True
                bRequiredActions += 1
                GoTo Common_Exit
            End If


            If Not name.Contains("*") And ext = "*" Then
                bNameCompare = True
                bRequiredActions += 1
                GoTo Common_Exit
            End If

            If Not ext.Contains("*") And name = "*" Then
                bExtCompare = True
                bRequiredActions += 1
                GoTo Common_Exit
            End If

            If Not name.Contains("*") And name.Length > 0 Then
                bNameCompare = True
                bRequiredActions += 1
            End If

            If Not ext.Contains("*") And ext.Length > 0 Then
                bExtCompare = True
                bRequiredActions += 1
            End If

            'do a name scan


            SplitI(name, "*", NameDict)
            Select Case NameDict.Count
                Case Is = 1
                    bNameCompare = True
                Case Is > 1
                    bNamescan = True
                    bRequiredActions += 1
            End Select


            SplitI(ext, "*", ExtDict)
            Select Case ExtDict.Count
                Case Is = 1
                    bExtCompare = True
                Case Is > 1
                    bExtscan = True
                    bRequiredActions += 1
            End Select


    Common_Exit:



  • favio

    Surprisingly enough wild carding is complex. I've actually seen multipage papers from Microsoft describing their wildcarding. It is not simple.

    The complexity is in the frontend. That is breaking a request into substrings. The back end that does a search is a standard string search.

    I am not a mistress in parsing and do not pretend to be. But that's what is required in straight code to wild card a filename.

    Perhaps someone with expertise in regular expressions will come along with something simpler but I kind of doubt it. The microsoft paper on wildcarding was rather long.

    I do see where I could have used splits instead of loops. That would have made things MUCH shorter.

  • vdinenna

    How about regular expressions
    str1 = system.text.regularexpressions.regex.replace(str1,"Test.*", "Test")

    Very powerful and therefore complex, check out:
    http://www.regular-expressions.info/

    John.




  • Radi Naydenov

    Thanks for that, but im not too sure whether that will do what im after... Basically i am doing a find and replace for my application, and i want to support wildcards like the find and replace in VB.NET works. It doesnt need to be as complex, it just needs to support the and * wildcards.
  • Wildcard Replace using the VB.NET Replace function