What are Regular Expressions?
Rather than spend thousands of keystrokes explaining what regular expressions are, here’s what the Wikipedia has to say.


How can they be used in Visual FoxPro?
The main way in which VFP developers have utilized regular expressions (if at all) in their applications is via the Windows Scripting Host. You can see some more information on regular expressions and how the RegExp object is used in VFP code on the Fox.Wikis (Thanks to Alex Feldstein for most of the content).


While this solution is OK, there are a few gotchas such as the user account’s security settings getting in the way, or WSH not being available on the system. However, it is the best most of us could do if we didn’t have some spendy third party library to provide this functionality. But then I decided to try my hand at creating another solution that we could all benefit from.


Introducing the regexp.fll
I decided it was high time that Visual FoxPro developers had a free Visual FoxPro Dynamic-Link Library (FLL) that would provide for regular expressions in Visual FoxPro, so I sat down at my copy of Visual Studio 8 and, with the help of Boost, I wrote one. Unfortunately the process wasn’t quite as straight forward as that last sentence makes it sound.

First I toyed with the idea of writing my own in pure Visual FoxPro code. That bad idea, while appealing to my reinvent the wheel side, was soon discarded because it would be too much effort and the end result would never be able to match the performance that could be achieved by doing this in C++. Next I researched all of the available C++ libraries that were available and for a time I was using GRETA, but between bugs in the code, the feeling that it wasn’t being supported quite as well since Eric Niebler left, and some disturbing things I read about the licensing regarding commercial applications, I soon discarded this as well.


After playing around with a half-dozen other libraries and comparing form, function, and performance of each I settled on Boost RegExp and the regexp.fll project took shape and was finished in short order. It probably took me as long to compile the Boost RegExp libraries as it did to write the actual code for this FLL.


Just a start
This library currently just does matching. While matching is arguably the thing regular expressions are used for most, there are a lot of other things that I will be adding to this library that will allow us to do a whole host of things in Visual FoxPro using regular expressions, such as return match sets, perform positional searches, and do replacements. Even though the library doesn’t provide for this other functionality, I think you will find its current matching capabilities pretty useful.

Important note about the required C++ runtime libraries
In order to use this FLL (which was compiled with Visual Studio 8), you will need the correct runtime libraries and they have to be placed on the system in a very specific way. I can’t tell you the number of emails I’ve gotten for the vfpencryption.fll that I wrote where developers were having problems getting it to work because they didn’t have the runtimes or the runtimes weren’t installed on the system correctly. I’m not saying I minded the emails (it’s always good to hear from a member of the Visual FoxPro Community), or that I don’t understand the confusion… I’m just saying that with Visual C++ 8.0 some of the rules have changed and we all have to learn the correct way to go about doing this in the age of .NET 2.0.


The easiest way to get the correct C++ runtime libraries installed on your system (or a user’s system) is to just download the installer (2.48 MB) that Microsoft has provided. It has some bloat to it, but it is point and click easy and the libraries all get installed where they should be without much hassle or fuss.


Now, for those of you that want to create install sets or just want to have the bare minimum required you will want to create a subdirectory named “Microsoft.VC80.CRT” in the same directory that the regexp.fll is in. The Microsoft.VC80.CRT subdirectory needs to have 4 files in it: Microsoft.VC80.CRT.manifest, msvcm80.dll, msvcp80.dll, and msvcr80.dll. In order to help you set this up I have created a zip file of this folder (577 KB) that you can download. You don’t have to register any of these files or do anything additional other than provide them as I have specified here. The directory structure should look like this…



As you can see, both the Microsoft.VC80.CRT and regexp.fll are in the same directory (My Application). And, inside of the Microsoft.VC80.CRT subdirectory are the previously mentioned 4 files. This is the way to do this for any FLL that I release. (truth be told there are other ways to do this, but then I have to go into a whole discussion about the WinSxS directory and what to do on pre-XP versions of Windows… life is too short)


Using the regexp.fll in Visual FoxPro
The first thing to do is download the FLL at the link provided at the bottom of this blog entry and make sure that you have the C++ runtimes installed as described in one of the two ways above. Then you can start using it from within Visual FoxPro or your Visual FoxPro applications. The syntax is very straightforward and there is only one function to remember… RegExp(cString, cRegExpression). The cString parameter is the string you want searched for a match, and the cRegExpression is the regular expression you want used when matching. RegExp() will return .T. for a match and .F. for no match. It might help to see some simple coded examples (cut-n-paste the code below into a prg and execute it)…



CLEAR


*!* Let Visual FoxPro know about the new function
*!* by setting the library to the regexp.fll
SET LIBRARY TO LOCFILE(“regexp.fll”,”FLL”,”Locate regexp.fll”)


LOCAL lcExpression
*!* Validate Email addresses
lcExpression = “^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$”
?RegExp(“
john@isp.com”,lcExpression)
?RegExp(“
john@.isp.com”,lcExpression)


*!* Validate currency amounts
lcExpression = “^(\$)?(([1-9]\d{0,2}(\,\d{3})*)|([1-9]\d*)|(0))(\.\d{2})?$”
?RegExp(“$1,244,311.81”,lcExpression) && Match
?RegExp(“$1,24,4311.81”,lcExpression) && No Match


*!* Validate days of the week
lcExpression = “^(Sun|Mon|(T(ues|hurs))|Fri)(day|\.)?$|Wed(\.|nesday)?$|Sat(\.|urday)?$|T((ue?)|(hu?r?))\.?$”
?RegExp(“Sunday”,lcExpression) && Match
?RegExp(“sundae”,lcExpression) && No Match


*!* Validate phone numbers
lcExpression = “^[2-9]\d{2}-\d{3}-\d{4}$”
?RegExp(“507-562-0020”,lcExpression) && Match
?RegExp(“507-56-0020”,lcExpression) && No Match


*!* release the library reference
SET LIBRARY TO


Boost documentation for RegExp
You can find the documentation for the RegExp’s syntax on the Boost website (you’ll note that it is based on the Perl regular expression syntax). You can also find a whole host of other examples, information and how-tos for regular expressions by doing a quick web search.


Here’s the link to download the regexp.fll
I’ve included the Boost license as a ReadMe.txt with this FLL. It isn’t necessary to distribute this disclaimer with the FLL as the FLL is a compiled library, however I am including it so you can see what the license says – the basic point is that we are free to use this commercially. If you want more information on the license requirements for Boost.

Download the regexp.fll (47 KB approx)