Thursday, August 13, 2009

Yes you can use Regular Expressions in MS-Access

I've heard it was not possible, but it is:


Private Function testRE()
Dim re As Object, strText as String
strText = ""the short brown fox, jumped over " _
& "the Foxy lad of foxtown in Fox-ville."
Set re = CreateObject("vbscript.regexp")
Dim colMatches
With re
.Pattern = "\bfox\b"
.IgnoreCase = True
.Global = True
End With
'Debug.Assert False
Dim myNum
Set colMatches = re.Execute(strText)
Debug.Print colMatches.Count & " matched"
Dim inttemp As Integer
For inttemp = 0 To colMatches.Count - 1
Debug.Print ">" & colMatches(inttemp)
Next inttemp

Debug.Print re.Replace(strText, "Dog")

End Function


The results:



results:
2 matched
>fox
>Fox
the short brown Dog, jumped over the Foxy lad of Newtown foxtown in Dog-ville.

2 Comments:

At Thu Aug 13, 08:24:00 PM PDT , Blogger Finley Lee said...

Totally awesome!

 
At Fri Aug 14, 06:34:00 AM PDT , Blogger Michael said...

That's what I thought. Everywhere I'd seen questions about re in Access, people said, it's not possible, but there it is, and it works!

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home