vendredi 20 novembre 2015

VBA regexp to check special symbols

I have tried to use what I've learnt in this post, and now I want to compose a RegExp which checks whether a string contains digits and commas. For example, "1,2,55,2" should be ok, whereas "a,2,55,2" or "1.2,55,2" should fail test. My code:

Private Function testRegExp(str, pattern) As Boolean
    Dim regEx As New RegExp

    If pattern <> "" Then
        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .pattern = pattern
        End With

        If regEx.Test(str) Then
            testRegExp = True
        Else
            testRegExp = False
        End If
    Else
        testRegExp = True
    End If
End Function

Public Sub foo()
    MsgBox testRegExp("2.d", "[0-9]+")
End Sub

MsgBox yields true instead of false. What's the problem ?

Aucun commentaire:

Enregistrer un commentaire