Results 1 to 9 of 9

Thread: Regular Expressions

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Posts
    28
    Does VB6 support regular expressions?

  2. #2
    Guest
    What Exactly do you mean by "Regular Expression"??

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Posts
    28
    Something of the sort:
    Code:
    Instr([a-Z][^0-9])
    or is there a special VB keyword for it?

  4. #4
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    I've never seen REGEX support

    However, I was looking for Regular Expression support in Java once and ended up finding some GNU classes for that purpose. They were a little clumsy to use when compared with LISP, but they got the job done.

    I would hazard a guess and say that someone would have made VB classes or libraries for regex by now... Not that I can tell you who or where...

    I can tell you that native support for regular expressions as per your example is definitely not part of VB6. More's the pity!

    Cheers
    Paul Lewis

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Posts
    28
    No doubt about that... I found references to them on the MSDN CD, but only for VBscript and Jscript, do you think it may be possible somehow to exploit any of their resources in a VB project?

  6. #6
    New Member
    Join Date
    Aug 2000
    Location
    Victoria BC Canada
    Posts
    3
    You can use Regular Expressions in VB6. I use them in
    Access 97. Here are some of my notes. Version 5.5 only
    released last week allows for sub expressions. I can't
    find any new help release only have VbScrip.chm.

    Ok. Here is how to get at Regular Expressions from Access 97. Modify for VB6 or VB5
    Wish I had known this a year ago.

    I assume you have installed Windows Scripting Host 2.0 fom Microsoft at
    http://msdn.microsoft.com/scripting/

    This will give you access to vbscript.dll
    or You may have DLL from IE5 or IE5.5

    If you go to the tools reference window and search for this file with
    browse and check off vbscript.dll. You only get access to Microsoft
    VBScript Globals. So you have to force a reference to be added with
    code to get regular expressions references. Here is code to do it.


    Function ReferenceFromFile(strFileName As String, _
    Optional Warning As Boolean) As Boolean

    ' One caveat adding a reference this way forces a decompile
    ' so cannot be used in an MDE. At least not executed inside one to
    ' reset references.

    Dim ref As Reference

    On Error GoTo Error_ReferenceFromFile
    Set ref = References.AddFromFile(strFileName)
    ReferenceFromFile = True

    Exit_ReferenceFromFile:
    Exit Function

    Error_ReferenceFromFile:
    If Warning = True Then MsgBox Err & ": " & Err.Description
    ReferenceFromFile = False
    Resume Exit_ReferenceFromFile
    End Function


    Now call this function as follows depending on version required either
    1.0 or 5.5 of VBScript Reqular Expressions. Version 5.5 has extra
    objects to handle Multiline or SubExpressions.

    Version 1.0

    ReferenceFromFile "c:\Windows\System\vbscript.dll\2" , True

    or
    Version 5.5

    ReferenceFromFile "c:\Windows\System\vbscript.dll\3" , True

    You will see the references appear in your tools reference list or
    look in Object browser.

    I tested with this routine borrowed from Dev Ashish for Access 2000
    Regular Expressions. Validates a zip code.

    ' ****** Test Code Start *******
    Sub sTestZipCodes()
    Const ERR_ZIPCODE = "Must be a valid 5 or 9 digit zip code"
    Debug.Print fValidateZipcode("06905")
    Debug.Print fValidateZipcode("06905-0011")
    Debug.Print fValidateZipcode("zxf44")
    Debug.Print fValidateZipcode("000922")
    End Sub

    Function fValidateZipcode(strZipCode As String) As Boolean
    On Error GoTo ErrHandler
    ' Windows 2000: Requires a reference to VBScript Regular Expressions
    ' Microsoft VBScript Regular Expressions 1.0
    ' VBScript_RegExp - C:\WINNT\System32\vbscript.dll\2 Version 1.0
    ' C:\Windows\System\vbscript.dll\2
    ' Microsoft VBScript Regular Expressions 5.5
    ' VBScript_RegExp - C:\WINNT\System32\vbscript.dll\3 Version 5.5
    ' C:\Windows\System\vbscript.dll\3
    ' if using Version 5.5
    ' Dim objRE As VBScript_RegExp_55.RegExp

    Dim objRE As VBScript_RegExp_10.RegExp

    Set objRE = New VBScript_RegExp_10.RegExp
    With objRE
    .Pattern = "^\d{5}([ -]?\d{4})?$"
    fValidateZipcode = (.Test(strZipCode))
    End With
    Set objRE = Nothing
    ExitHere:
    Exit Function
    ErrHandler:
    fValidateZipcode = False
    With Err
    MsgBox "Error: " & .Number & vbCrLf & .Description, _
    vbCritical Or vbOKOnly, .Source
    End With
    Resume ExitHere
    End Function


    You can start collecting Regular Expressions for
    various common form elements in a file, for instance here are some
    untested ones and possibly uncomplete:

    var PatternsDict = new Object();

    PatternsDict.zipPat = "^\d{5}(-\d{4})?$"
    ' matches zip codes

    PatternsDict.currencyPat = "/^\$\d{1,3}(,\d{3})*\.\d{2}$"
    ' matches $17.23 or $14,281,545.45 or ...

    PatternsDict.timePat = "^\d{2}:\d{2}$"
    ' matches 12:34 but also 75:83

    PatternsDict.timePat2="^([1-9]|1[0-2]):[0-5]\d$"
    ' matches 5:04 or 12:34 but not 75:83

    PatternsDict.PhonenumbersUSCanada = "^(\(\d{3}\) )?\d{3}-\d{4}$"

    PatternsDict.FloatingPoint= "(\+|-)?(\d+\.?\d*|\.\d+)([eE](\+|-)?\d+)?"
    This seemingly cryptic pattern is actually a true test for a float or
    double that might include the dreaded "E". Note that essentially,
    the pattern is divided into three subsections,
    first and last being optional, although not testing if
    the number actually begins with a zero or not

    URL

    (?:http://(?:(?:(?:(?:(?:[a-zA-Z\d](?:(?:[a-zA-Z\d]|-)*[a-zA-Z\d])?)\.

    Email

    "^([\w-_]+\.)*[\w-_]+\@ ([\w-_]+\.)+[a-zA-Z]{2,3}$"
    This pattern is a simple "email address" match.

    A comprehensive "email address" pattern is a little bit bigger
    than this, due to additional rules imposed on email addresses
    (including a name and so on). However, it would be possible to provide
    an even more lenient email match with "^.+\@.+\..+$" .
    Note that this pattern will not match email addresses
    that use an IP address instead of a fully qualified domain name.

    Before anyone asks for comprehensive patterns for Email or URL parsing
    expressions.Some are available from Perl or ASP sites or if you
    look at the source of some OS's like Linux.
    They generally run about 6-10K bytes.
    There is a comprehensive one for EMail in one of the OWL books
    O'Reilly's "Mastering Regular Expressions"
    or try for more info
    http://msdn.microsoft.com/workshop/l...ting051099.asp
    Last edited by MartinLiss; Apr 28th, 2008 at 11:29 AM. Reason: Disable smilies in text
    Marty Connelly Victoria, BC Canada

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Can you turn smilies off and repost that, please .
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    New Member
    Join Date
    Aug 2000
    Location
    Victoria BC Canada
    Posts
    3
    Here are Regular Expressions without smilies

    URL

    (?:http://(?:(?:(?:(?:(?:[a-zA-Z\d](?:(?:[a-zA-Z\d]|-)*[a-zA-Z\d])?)\.
    or
    .Pattern = "(http://|www)(.+/*.*)+"
    or
    .Pattern = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?"

    or Here is Canadian Zip Code Regular Expression
    'V8T-3B8 or V8T 3B8 or V8T3B8 will parse OK

    .Pattern = "^[A-Z][0-9][A-Z][-\s]?[0-9][A-Z][0-9]"



    Marty Connelly Victoria, BC Canada

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Thanks for that. After marvelling at the power of regex in Perl, finding a way of doing it in VB is a happy thought.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width