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