Using Regular Experssions

marlan

Registered User.
Local time
Today, 14:32
Joined
Jan 19, 2010
Messages
415
Hi All you experts!
I used this code by Guus2005, and this page to have better understanding of the oMatches Collection.
Can any one help me get to oMatches(1)? I tested the code on a string with a few matces, it only gave me the first one.
example: the pattern (https?://|www[1-9]?\.[A-Z0-9\.-]+\.[A-Z]{2,})
on the string : "if you find a URL, such as www.access-programmers.co.uk/forums/showthread.php?t=233789 or www.gmail.com, please let me know ASAP. http://newmail.walla.co.il/ is valid too."
Code:
     For Each oMatch In oMatches
        i = i + 1
        MsgBox CStr(i) & ": " & Chr(13) & "Value = " & oMatch.Value & Chr(13) & _
                    "FirstIndex = " & oMatch.FirstIndex & Chr(13) & _
                    "length = " & oMatch.length
    Next oMatch
gave me only one msgBox with the first match (not 3).

Can anyone pleae help me on getting to the other matches? I guess it has somthing to do with tha fact it is a VBscript object running in Access. PS: the pattern is good (I checked it on all the above URLs).
 
Please show all your code. You are working with 2 functions from Guus, so it isn't clear , to me at least, what your code looks like.

I copied the 2 functions from Guus' work and then created a small procedure to test your urls.

You seem to be using Matches which may mean you are not using Guus' functions???

Here is my test procedure.

Code:
'---------------------------------------------------------------------------------------
' Procedure : jDEC25_2012
' Author    : Jack
' Date      : 25-12-2012
' Purpose   : To test some URLs for pattern match (regex) using some functions
' provided by Guus AWF
'---------------------------------------------------------------------------------------
' Last Modified:
'
' Inputs: N/A
' Dependency: This proc calls 2 functions provided by Guus
'
' Regexp
' RegexpBln
'--------------------------------------------------------------------------
'
Sub jDEC25_2012()
      'based on post
      'http://www.access-programmers.co.uk/forums/showthread.php?t=239102

      Dim MyPattern As String '
10    MyPattern = "(https?://|www[1-9]?\.[A-Z0-9\.-]+\.[A-Z]{2,})"
      Dim myUrl(4) As String
      Dim i As Integer
20    i = 0
30    myUrl(0) = "http://www.access-programmers.co.uk/forums/showthread.php?t=233789"
40    myUrl(2) = "http://newmail.walla.co.il/"
50    myUrl(1) = "http://www.gmail.com/"
60    myUrl(3) = "http://strategis.ic.gc.ca"
70     For i = 0 To 3
80         Debug.Print i & "   Value = " & myUrl(i) & "   " _
                       & RegexpBln(myUrl(i), MyPattern)
90     Next i
End Sub
 
Hi jdraw, and thanks for your reply.

I've modified a bit Guss' Regexp:
Code:
Public Function Regexp(strData As String, strPattern As String) As String
    On Error GoTo Error_Here
    Dim oRegexp As Object, oMatches As Object, oMatch As Object
    Dim i As Integer
    If Len(strData) = 0 Or Len(strPattern) = 0 Then Exit Function

    Set oRegexp = CreateObject("vbscript.regexp")
    With oRegexp
        .MultiLine = False
        .Global = False
        .IgnoreCase = True
        .Pattern = strPattern
    End With
     
    Set oMatches = oRegexp.Execute(strData)
    For Each oMatch In oMatches
        i = i + 1
        MsgBox CStr(i) & ": " & Chr(13) & "Value = " & oMatch.Value & Chr(13) & _
                    "FirstIndex = " & oMatch.FirstIndex & Chr(13) & _
                    "length = " & oMatch.length
    Next oMatch
    
    Regexp = oMatches(0)

Exit_Here:
    Exit Function
Error_Here:
    Regexp = ""
    Resume Exit_Here
End Function
I would like to see other Matches.
Attachd is a pic of the output
 

Attachments

  • RegEx.JPG
    RegEx.JPG
    22.5 KB · Views: 105
Last edited:

Users who are viewing this thread

Back
Top Bottom