# Wednesday, January 11, 2006

Just the beginning
In an earlier blog entry I unveiled a new FLL for Visual FoxPro that would allow Visual FoxPro developers to use Regular Expressions for matching. That FLL, while very useful if you are competent with regular expressions, was just the start of what's possible. Now that I have a basic library that exposes part of the functionality of Boost Regex, I can expand it to include additional functionality. I believe you'll find the additions, which include formatting and splitting useful.

Download the regexp.fll (61 KB approx)


 regexp.fll Documentation...


Function REGEXP()

Signature: RegExp(cString, cRegularExpression [, nFunctionType[, cFormatExpression | cArrayName]])

Parameters:

cStringtoEncrypt - The string to be searched, formatted, or split

cRegularExpression - A regular expression using Perl syntax. Additional documention.

nFunctionType - There are currently 3 function types available. The value of this parameter determines the operation that RegExp will perform as well as its return value.

List of currently supported functions:

  • 0 = Pattern Match - RegExp() returns .T. if a pattern is found in cString that matches the cRegularExpression, otherwise .F. is returned signifying that no match was found. This is the default behavior for RegExp() when only the first two parameters are sent in.
  • 1 = Format - RegExp() returns a formatted character string that is created by iterating through cString looking for matches of cRegularExpression and applying cFormatExpression to those matches found.
  • 2 = Split - RegExp() returns the number of splits when it has create an array of the patterns it has found in cString that match the cRegularExpression. The name of the array is specified by the parameter cArrayName.

cFormatExpression | cArrayName - Depends on the value of the nFunctionType parameter

  • If nFunctionType is 1, then this parameter is a perl-like format string. Additional documentation.
  • If nFunctionType is 2, then this parameter is the name of the array to be created.

Return Value:

Depends on value of the nFunctionType parameter (see documentation for nFunctionType parameter above). RegExp() will return a logical value indicating whether a match was found if nFunctionType is 0. RegExp() will return a formatted character string if nFunctionType is 1. RegExp(), after succefully creating the array of splits will return the number of elements (number of splits) if nFunctionType is 2.

Example of use: (cut-n-paste the following code into a PRG and execute it from within Visual FoxPro)

*!* The following simple example shows how to use the RegExp()
*!*
function to validate, format, and split credit card numbers

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
lcExpression = "\A(\d{3,4})[- ]?(\d{4})[- ]?(\d{4})[- ]?(\d{4})\z"

? "Pattern Matches:"
*!* Validate credit card number using RegExp()
?RegExp("1234-5678-9101-1121",lcExpression) && Match Found
?RegExp("123-567-910-112", lcExpression) && No Match Found
?

?"Formatting:"
*!* Format credit card number using RegExp()
?RegExp("1234567891011121",lcExpression, 1, "\1-\2-\3-\4") && Match Found
?RegExp("12345678910111", lcExpression, 1, "\1-\2-\3-\4") && No Match Found
?

*!* Split credit card number using RegExp()
?"Splits: " + TRANSFORM(RegExp("1234-5678-9101-1121",lcExpression, 2, "arySplits")) && Matches Found
ShowSplits()
?"Splits: " + TRANSFORM(RegExp("1234-5678-9101-112", lcExpression, 2, "arySplits")) && No Matches Found
ShowSplits()

*!* release the library reference
SET LIBRARY TO

***************************
Procedure ShowSplits()
***************************
 LOCAL lnCounter
 FOR lnCounter = 1 TO ALEN(arySplits)
  ? CHR(9) + TRANSFORM(lnCounter) + ": " + arySplits(lnCounter)
 ENDFOR
 ?
ENDPROC


Wednesday, January 11, 2006 5:58:48 AM (GMT Standard Time, UTC+00:00)  #    Comments [9]
Wednesday, January 11, 2006 9:55:06 PM (GMT Standard Time, UTC+00:00)
Thanks Craig! I am already using your regex.fll in my application. Recently I need regex capability in my app and I thought of using the vbscript.regexp object, but eventual I use regex.fll instead because I don't have to deal with the object creation/release in code, especially I need the regex capability in a utility function being called repeated in a tight loop. Deployment is straigh forward too.
David Fung
Thursday, January 12, 2006 8:10:39 AM (GMT Standard Time, UTC+00:00)
High Craig,
Thanks for your efforts. This utility surely is a very usefull addition to VFP. Have you run any performance comparison tests between regexp.fll and vbscript.regexp ?
Dimitrios Papadopoulos
Thursday, January 12, 2006 9:41:58 AM (GMT Standard Time, UTC+00:00)
Hi David,
Great to hear! Thanks for stopping by and taking the time to post your thoughts regarding the regexp.fll. I'm glad you found the deployment straight forward, some have been having problems figuring out where and how to include the C Runtimes. However, I'm hoping that my blog entry when I released the regexp.fll helped to clear up some of the confusion.

Hi Dimitrios,
I haven't run any performance comparisons between the two. If you (or anyone else reading this) gets some time, maybe you could run out some benchmarks between the two. Good question though.
Friday, January 13, 2006 7:25:24 PM (GMT Standard Time, UTC+00:00)
Hi Craig,
Your detail blog entry about deployment did help me. I found out one thing today: in my case, although the "copy microsoft.vc80.crt folder" method works well for Win2K/WinXP, it does not work for Win98 client workstations (my app is in a shared network location). For Win98, have to run the Microsoft VC++ redist installer on the Win98 workstations to make it works.
David Fung
Friday, January 20, 2006 9:17:24 AM (GMT Standard Time, UTC+00:00)
Hi,
I'm new to VFP so I may have done something stupid to cause it not to work.

I am getting:
Program Error
Library File c:\program files\microsoft visual foxpro 9\regexp.fll is invalid.

Is this expected to work for VFP 9?

Here's what I did:

I dropped the regexp.fll file into the VFP install dir as shown above. new project, new program, paste the example code, run.

Anyone know what's wrong?

Thanks!

- g
kaon
Saturday, January 21, 2006 12:09:41 AM (GMT Standard Time, UTC+00:00)
Hi kaon,

The instructions for how to properly install the runtime libraries for this example are provided in the following blog entry:

http://www.sweetpotatosoftware.com/SPSBlog/PermaLink,guid,91241006-595a-487d-ac06-d0fc1fc71632.aspx
Monday, January 23, 2006 10:53:37 AM (GMT Standard Time, UTC+00:00)
Hi Craig,
It worked after I pasted the Microsoft.VC80.CRT.
Totally painless.

THANK YOU!

(Would've been nice if VFP could have displayed a more informative error message.)
kaon
Friday, March 24, 2006 2:34:06 PM (GMT Standard Time, UTC+00:00)
Craig,

I've been waiting for an opportunity to try this out and now I have a use for it. Do you have any plans to integrate search/replace into the fll?

Thanks again for all your contributions to the community!

RK
Sunday, March 26, 2006 7:31:19 AM (GMT Daylight Time, UTC+01:00)
Hi Richard,

Search and replace, though perhaps not self-evident from the examples I've posted so far, is already provided for by the RegExp() function. Take the following code for example...

SET LIBRARY TO LOCFILE("regexp.fll","FLL","Locate regexp.fll")
LOCAL lcExpression
lcExpression = "[1]" && matching a specific character
?RegExp("1234515678198761",lcExpression, 1, "ONE")
SET LIBRARY TO

While a simplistic example, the code will produce the following string (1 is replaced with ONE)...

ONE2345ONE5678ONE9876ONE

So, this works to search and replace (as in this example) or search and format (as in the example I provided using a credit card number). All depends on what is provided for the regular expression and the format expression. It should provide for just about the most complex replacing or formatting needs that anyone could think up.
All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview

 

Archive

<October 2014>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678