1

I have this function that I have used for a while that removes tabs and other unwanted things from my strings. However I recently wanted to adapt it for using windows paths, so I want to change it so it leaves the : and \ characters in strings. I believe all I need to add the character "\" to the .Pattern line. However I've tried the code below and it doesn't work for some reason, can anyone help?

many thanks

Public Function PrepareString(TextLine As String) As String

        Dim oRegex As Object
        If oRegex Is Nothing Then oRegex = CreateObject("vbscript.regexp")
        With oRegex
            .Global = True
            'Allow A-Z, a-z, 0-9, -, /, :, _ and ' 
            .Pattern = "[^A-Za-z0-9 -/\:_']"
            TextLine = .Replace(TextLine, vbNullString)
        End With
        TextLine = Trim(TextLine)

        PrepareString = TextLine

    End Function

1 Answer 1

1

In RegEx, \ is a special character, so you have to escape it with a \.

Try this:

.Pattern = "[^A-Za-z0-9 -/\\:_']"

That would be the *nix way, at least

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.