Consulting

Results 1 to 5 of 5

Thread: regex function isn't working

  1. #1

    regex function isn't working

    I can't seem to figure out why my regular expressions function isn't working. Can anyone help?
    [VBA]Sub t()
    Debug.Print regex_match("MA-167", "[\D{2}-\d{3}]")
    End Sub
    Function regex_match(value As String, _
    pattern As String, _
    Optional multiline = False, _
    Optional globl = False, _
    Optional ignoreCase = False) As String

    Dim re As Object, REMatches As Object

    Set re = CreateObject("vbscript.regexp")

    With re
    .multiline = multiline
    .Global = globl
    .ignoreCase = ignoreCase
    .pattern = pattern
    End With

    Set REMatches = re.Execute(value)
    regex_match = REMatches(0)
    End Function[/VBA]

    error is here: Set REMatches = re.Execute(value)

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I think your pattern is invalid.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    FWIW, I would suggest testing before .Execute. That way (with a valid pattern), it doesn't hang upon no match.

    [VBA]Sub t()
    Debug.Print regex_match("Mary had a model MA-167", "\b\D{2}-\d{3}\b")
    Debug.Print regex_match("Tom had a model af-237, which is newer", "\b\D{2}-\d{3}\b")
    Debug.Print regex_match("Mary also had a model XMA-167", "\b\D{2}-\d{3}\b")
    'If always two letters,hyphen,three digits, maybe...
    Debug.Print regex_match("Tom also had a model XX-237, which is older.", "\b[a-zA-Z]{2}-\d{3}\b")
    End Sub

    Function regex_match(value As String, _
    pattern As String, _
    Optional multiline = False, _
    Optional globl = False, _
    Optional ignoreCase = False) As String

    Dim re As Object, REMatches As Object

    Set re = CreateObject("vbscript.regexp")

    With re
    .multiline = multiline
    .Global = globl
    .ignoreCase = ignoreCase
    .pattern = pattern

    If .Test(value) Then
    regex_match = re.Execute(value)(0)
    Else
    regex_match = "No Return"
    End If
    End With
    End Function[/VBA]

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by GTO
    FWIW, I would suggest testing before .Execute. That way (with a valid pattern), it doesn't hang upon no match.
    No, but it does with an invalid pattern
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by xld
    No, but it does with an invalid pattern
    Sorry, I meant to say that, but was less than clear

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •