Wiki Home

Reg Exp


Namespace: VFP
Best article I know on RE are the perl docs [and related links at the bottom]:
http://www.perl.com/doc/manual/html/pod/perlre.html

Has anybody else read this article on regular expressions?
http://www.eps-cs.com/VFPConversion/Article.aspx?quickid=0305041
You know, I used to think Jim Duffy was a pretty smart guy but after reading
this I'm not sure. I wonder if anyone tested his examples before he published
this article. I found his comments to be riddled with errors.

Regular Expressions parser built into Windows Script Host (WSH)

Simple example:
* Password validation
* Expression:  ^[a-zA-Z]\w{5,14}$
* Description:   The password's first character must be a letter,
* 				it must contain at least 6 characters and no more
*				than 15 characters and no characters other than
*				letters, numbers and the underscore may be used.
* Notes: Simple example, not for bulletproof passwords which would have to
* accept non-regular alphabet characters, spaces allowed for passphrases, etc.

lparameters tcPassword
tcPassword = Alltrim(tcPassword)

Local oRE
oRE = CreateObject("VBScript.RegExp")
oRE.Pattern = "^[a-zA-Z]\w{5,14}$"
llresult = oRE.test(tcPassword)

-- Alex Feldstein
Do a second test to require at least one numeric digit
oRE.Pattern2 = "\d"
llresult = llresult and oRE.test(tcPassword)

-- Brian Marquis
* validate an email address
* there are many ways
* to do this. This is just one of them
Local oRE
oRE = CreateObject("VBScript.RegExp")
oRE.Pattern = '^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$'
llresult=oRE.test("abc@xyz.org")

-- Alex Feldstein
Validate US SSN
cSSN = [999-99-9999]
oRE = CreateObject("VBScript.RegExp")
oRE.Pattern = "^(?!000)([0-6]\d{2}|7([0-6]\d|7[012]))([ -]?)(?!00)\d\d\3(?!0000)\d{4}$"
llresult = oRE.test(cSSN)

-- Alex Feldstein
An article titled Regular Expressions Primer at: http://developer.com/lang/article.php/3330231
See also:
  • VFUG -- March 2001 VFUG Newsletter The Windows Script Host (Pt 5 of 5) -- Regular Expression Parser--
    Article by George Tasker and Ed Rauh

    RegExp.fll - Free, feature rich library for working with regular expressions in Visual FoxPro

    Great resource: RegexLib.com -- a Regular Expression Library. Excellent.

    Also see a series of articles on parsing with regexp in VFP, in which the strtranx() function is developed which allows regular expression matches to be replaced with results from VFP expressions, possibly parameterized by submatches in the pattern provided, in one line of code. - Lauren Clarke

    *-- add 1 to all digits
    
    ? strtranx( [ABC123], "(\d)", ;
                [=TRANSFORM(VAL($1)+1)] )
    
    ** prints "ABC234"
    
    
    *-- convert all dates to long format
    ? strtranx( [the date is: 7/18/2004 ] ,;
                [(\d{1,2}/\d{1,2}/\d{4})],;
                [=TRANSFORM(CTOD($1),"@YL")])
    
    ** prints "the date is: Sunday, July 18, 2004"
    

    Great examples everyone! Here's one to validate a UK postcode. The Royal Mail accepts the following 6 types:

    "A" indicates an alphabetic character and "N" indicates a numeric character.
    
    Format   Example Postcode
    
    AN NAA   M1 1AA
    ANN NAA  M60 1NW
    AAN NAA  CR2 6XH
    AANN NAA DN55 1PT
    ANA NAA  W1A 1HP
    AANA NAA EC1A 1BB
    
    ^[A-Za-z]{1,2}\d{1,2}[A-Za-z]? \d{1}[A-Za-z]{2}
    

    First, I managed '^[A-Za-z]{1,2}\d{1,2} \d{1}[A-Za-z]{2}' but I couldn't work out the last two formats, where an extra letter has been added: ANA and AANA. Then, I discoverd the ? meta-character, which indicates that the thing immediately to the left should be matched 0 or 1 times in order to be evaluated as true.

    -- Garry Bettle

    A great tool for creating and testing complex (or any) regular expressions, is RegExBuddy which works hand in hand with EditPadPro, a great text editor by the same developers. I recommend it highly. -- Alex Feldstein
    An Introduction to Perl Regular Expressions
    Also see the NET4COM Regular Expression with the Sedna October CTP
    -- Rhodri C Evans
    Contributors: Alex Feldstein
    Category Definitions Category Code Samples
  • ( Topic last updated: 2007.04.28 01:21:23 PM )