The Jackle
The Jackle

Reputation: 25

VBA regex - How to exclude special characters

I'm new to regex and I'm trying to exclude certain special characters from the replace function below.

Function removeAlpha(r As String) As String
With CreateObject("vbscript.regexp")
   .Pattern = "\D+"
    .Global = True
    removeAlpha = .Replace(r, "")
End With
End Function

Currently it's stripping out every single non-numeric within the string and leaving me with numbers.

However i want it to ignore the following characters - , .

Your help is much appreciated!

Upvotes: 0

Views: 1745

Answers (1)

Rahul
Rahul

Reputation: 2738

Include them in negated character class like this.

Regex: [^\d,.-]+ This will match not match more than one numeric, comma, dot and hyphen. \D is same as [^\d]

Note:- Always keep - at beginning or at end in a character class.

Upvotes: 1

Related Questions