Welcome to SPN

Register and Join the most happening forum of Sikh community & intellectuals from around the world.

Sign Up Now!

How to use Regular Expressions of VBScript?

Discussion in 'Information Technology' started by Danny J. Lesandrini, Jul 28, 2006.

  1. Danny J. Lesandrini

    Danny J. Lesandrini
    Expand Collapse
    Guest

    Anyone know how to get started using the Regular Expressions stuff
    of VBScript from an Access VBA Code module?

    If not the VBScript RegExp, then do you have another suggestion for
    testing strings to see if they match a particularly complex set of rules?
    --

    Danny J. Lesandrini
    dlesandrini@hotmail.com
    http://amazecreations.com/datafast
     
  2. Loading...

    Similar Threads Forum Date
    Controversial SGPC poll irregularities: report sought Hard Talk Aug 20, 2010
    Tatla is first turbaned Sikh regular officer in Canadian Air Force Sikh Youth Aug 22, 2008
    Sikh News Punjab govt ready for probe into "irregularities" in GNDU (Outlook India) Breaking News Mar 25, 2008
    Sikh News Punjab govt to regularise unauthorised water connections (New Kerala) Breaking News Nov 24, 2007
    Questions to ask yourself, regularly ! Spiritual Articles Jan 20, 2006

  3. Douglas J Steele

    Douglas J Steele
    Expand Collapse
    Guest

    Take a look at what John Nurick provided as a sample at
    http://www.mvps.org/access/modules/mdl0063.htm, or my October, 2003 "Access
    Answers" column (which you can download at
    http://www.accessmvp.com/DJSteele/SmartAccess.html)

    --
    Doug Steele, Microsoft Access MVP
    http://I.Am/DougSteele
    (no e-mails, please!)


    "Danny J. Lesandrini" <dlesandrini@hotmail.com> wrote in message
    news:eeTaK02eGHA.4892@TK2MSFTNGP02.phx.gbl...
    > Anyone know how to get started using the Regular Expressions stuff
    > of VBScript from an Access VBA Code module?
    >
    > If not the VBScript RegExp, then do you have another suggestion for
    > testing strings to see if they match a particularly complex set of rules?
    > --
    >
    > Danny J. Lesandrini
    > dlesandrini@hotmail.com
    > http://amazecreations.com/datafast
    >
    >
    >
     
  4. DBG

    DBG
    Expand Collapse
    Guest

    I'd guess you have found this already but if not, this is of relevance as well:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting051099.asp

    I don't use the late binding method in my project, I just hooked up the
    RegEx using a reference to the module mentioned at the top of John's article.

    Here's a really simple example:

    Dim strAString as String
    strAString = "111222AB"
    Dim re As New RegExp
    re.Pattern = "[A-Z][a-z]"
    re.IgnoreCase = True
    If (Not re.test(strAString)) Then
    ' String didn't contain a letter, do something
    ' Maybe Add or subtract
    else
    ' String contained a letter do something else
    end if

    In this example, the else case would fire because our string has a letter
    and our pattern says look for any letter.

    -David

    "Danny J. Lesandrini" wrote:

    > Anyone know how to get started using the Regular Expressions stuff
    > of VBScript from an Access VBA Code module?
    >
    > If not the VBScript RegExp, then do you have another suggestion for
    > testing strings to see if they match a particularly complex set of rules?
    > --
    >
    > Danny J. Lesandrini
    > dlesandrini@hotmail.com
    > http://amazecreations.com/datafast
    >
    >
    >
    >
     
  5. Danny J. Lesandrini

    Danny J. Lesandrini
    Expand Collapse
    Guest

    David, the question I have is this: What library reference do I need to set in order
    to use the RegExp object? I tried this code, but it failed to compile because the
    object reference couldn't be found.
    --

    Danny J. Lesandrini
    dlesandrini@hotmail.com
    http://amazecreations.com/datafast


    "DBG" <DBG@discussions.microsoft.com> wrote ...
    > I'd guess you have found this already but if not, this is of relevance as well:
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting051099.asp
    >
    > I don't use the late binding method in my project, I just hooked up the
    > RegEx using a reference to the module mentioned at the top of John's article.
    >
    > Here's a really simple example:
    >
    > Dim strAString as String
    > strAString = "111222AB"
    > Dim re As New RegExp
    > re.Pattern = "[A-Z][a-z]"
    > re.IgnoreCase = True
    > If (Not re.test(strAString)) Then
    > ' String didn't contain a letter, do something
    > ' Maybe Add or subtract
    > else
    > ' String contained a letter do something else
    > end if
    >
    > In this example, the else case would fire because our string has a letter
    > and our pattern says look for any letter.
    >
    > -David
     
  6. DBG

    DBG
    Expand Collapse
    Guest

    I pointed to John's article which Douglas had linked for the name of it, but
    its:
    VBScript regular expression object (v 5.5)
    Which doesn't mention that It's
    C:\Windows\system32\vbscript.dll
    unless I browsed the article too quickly.
    should do it :)

    -David


    "Danny J. Lesandrini" wrote:

    > David, the question I have is this: What library reference do I need to set in order
    > to use the RegExp object? I tried this code, but it failed to compile because the
    > object reference couldn't be found.
    > --
    >
    > Danny J. Lesandrini
    > dlesandrini@hotmail.com
    > http://amazecreations.com/datafast
    >
    >
    > "DBG" <DBG@discussions.microsoft.com> wrote ...
    > > I'd guess you have found this already but if not, this is of relevance as well:
    > > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting051099.asp
    > >
    > > I don't use the late binding method in my project, I just hooked up the
    > > RegEx using a reference to the module mentioned at the top of John's article.
    > >
    > > Here's a really simple example:
    > >
    > > Dim strAString as String
    > > strAString = "111222AB"
    > > Dim re As New RegExp
    > > re.Pattern = "[A-Z][a-z]"
    > > re.IgnoreCase = True
    > > If (Not re.test(strAString)) Then
    > > ' String didn't contain a letter, do something
    > > ' Maybe Add or subtract
    > > else
    > > ' String contained a letter do something else
    > > end if
    > >
    > > In this example, the else case would fire because our string has a letter
    > > and our pattern says look for any letter.
    > >
    > > -David

    >
    >
    >
     
  7. Douglas J Steele

    Douglas J Steele
    Expand Collapse
    Guest

    I never bother setting a reference, preferring to use Late Binding.

    Dim reCurr As Object

    Set reCurr = CreateObject("VBScript.RegExp")

    If you're determined to use Early Binding, I do talk about how to set the
    reference in the Smart Access article I cited (AFAIK, the only way to add
    the reference is using the AddFromGUID method.)

    --
    Doug Steele, Microsoft Access MVP
    http://I.Am/DougSteele
    (no e-mails, please!)


    "Danny J. Lesandrini" <dlesandrini@hotmail.com> wrote in message
    news:OyEIyK3eGHA.3792@TK2MSFTNGP03.phx.gbl...
    > David, the question I have is this: What library reference do I need to

    set in order
    > to use the RegExp object? I tried this code, but it failed to compile

    because the
    > object reference couldn't be found.
    > --
    >
    > Danny J. Lesandrini
    > dlesandrini@hotmail.com
    > http://amazecreations.com/datafast
    >
    >
    > "DBG" <DBG@discussions.microsoft.com> wrote ...
    > > I'd guess you have found this already but if not, this is of relevance

    as well:
    > >

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting051099.asp
    > >
    > > I don't use the late binding method in my project, I just hooked up the
    > > RegEx using a reference to the module mentioned at the top of John's

    article.
    > >
    > > Here's a really simple example:
    > >
    > > Dim strAString as String
    > > strAString = "111222AB"
    > > Dim re As New RegExp
    > > re.Pattern = "[A-Z][a-z]"
    > > re.IgnoreCase = True
    > > If (Not re.test(strAString)) Then
    > > ' String didn't contain a letter, do something
    > > ' Maybe Add or subtract
    > > else
    > > ' String contained a letter do something else
    > > end if
    > >
    > > In this example, the else case would fire because our string has a

    letter
    > > and our pattern says look for any letter.
    > >
    > > -David

    >
    >
     
  8. DBG

    DBG
    Expand Collapse
    Guest

    As an afterthought I'll probably change my code to use Late Binding as well.
    Although at this point most of the RegEx stuff is converted over to CLR
    Stored Procedures...

    -David


    "Douglas J Steele" wrote:

    > I never bother setting a reference, preferring to use Late Binding.
    >
    > Dim reCurr As Object
    >
    > Set reCurr = CreateObject("VBScript.RegExp")
    >
    > If you're determined to use Early Binding, I do talk about how to set the
    > reference in the Smart Access article I cited (AFAIK, the only way to add
    > the reference is using the AddFromGUID method.)
    >
    > --
    > Doug Steele, Microsoft Access MVP
    > http://I.Am/DougSteele
    > (no e-mails, please!)
    >
    >
    > "Danny J. Lesandrini" <dlesandrini@hotmail.com> wrote in message
    > news:OyEIyK3eGHA.3792@TK2MSFTNGP03.phx.gbl...
    > > David, the question I have is this: What library reference do I need to

    > set in order
    > > to use the RegExp object? I tried this code, but it failed to compile

    > because the
    > > object reference couldn't be found.
    > > --
    > >
    > > Danny J. Lesandrini
    > > dlesandrini@hotmail.com
    > > http://amazecreations.com/datafast
    > >
    > >
    > > "DBG" <DBG@discussions.microsoft.com> wrote ...
    > > > I'd guess you have found this already but if not, this is of relevance

    > as well:
    > > >

    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting051099.asp
    > > >
    > > > I don't use the late binding method in my project, I just hooked up the
    > > > RegEx using a reference to the module mentioned at the top of John's

    > article.
    > > >
    > > > Here's a really simple example:
    > > >
    > > > Dim strAString as String
    > > > strAString = "111222AB"
    > > > Dim re As New RegExp
    > > > re.Pattern = "[A-Z][a-z]"
    > > > re.IgnoreCase = True
    > > > If (Not re.test(strAString)) Then
    > > > ' String didn't contain a letter, do something
    > > > ' Maybe Add or subtract
    > > > else
    > > > ' String contained a letter do something else
    > > > end if
    > > >
    > > > In this example, the else case would fire because our string has a

    > letter
    > > > and our pattern says look for any letter.
    > > >
    > > > -David

    > >
    > >

    >
    >
    >
     
  9. DBG

    DBG
    Expand Collapse
    Guest

    As an afterthought I'll probably change my code to use Late Binding as well.
    Although at this point most of the RegEx stuff is converted over to CLR
    Stored Procedures...

    -David

    "Douglas J Steele" wrote:

    > I never bother setting a reference, preferring to use Late Binding.
    >
    > Dim reCurr As Object
    >
    > Set reCurr = CreateObject("VBScript.RegExp")
    >
    > If you're determined to use Early Binding, I do talk about how to set the
    > reference in the Smart Access article I cited (AFAIK, the only way to add
    > the reference is using the AddFromGUID method.)
    >
    > --
    > Doug Steele, Microsoft Access MVP
    > http://I.Am/DougSteele
    > (no e-mails, please!)
    >
    >
    > "Danny J. Lesandrini" <dlesandrini@hotmail.com> wrote in message
    > news:OyEIyK3eGHA.3792@TK2MSFTNGP03.phx.gbl...
    > > David, the question I have is this: What library reference do I need to

    > set in order
    > > to use the RegExp object? I tried this code, but it failed to compile

    > because the
    > > object reference couldn't be found.
    > > --
    > >
    > > Danny J. Lesandrini
    > > dlesandrini@hotmail.com
    > > http://amazecreations.com/datafast
    > >
    > >
    > > "DBG" <DBG@discussions.microsoft.com> wrote ...
    > > > I'd guess you have found this already but if not, this is of relevance

    > as well:
    > > >

    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting051099.asp
    > > >
    > > > I don't use the late binding method in my project, I just hooked up the
    > > > RegEx using a reference to the module mentioned at the top of John's

    > article.
    > > >
    > > > Here's a really simple example:
    > > >
    > > > Dim strAString as String
    > > > strAString = "111222AB"
    > > > Dim re As New RegExp
    > > > re.Pattern = "[A-Z][a-z]"
    > > > re.IgnoreCase = True
    > > > If (Not re.test(strAString)) Then
    > > > ' String didn't contain a letter, do something
    > > > ' Maybe Add or subtract
    > > > else
    > > > ' String contained a letter do something else
    > > > end if
    > > >
    > > > In this example, the else case would fire because our string has a

    > letter
    > > > and our pattern says look for any letter.
    > > >
    > > > -David

    > >
    > >

    >
    >
    >
     
  10. Jamie Collins

    Jamie Collins
    Expand Collapse
    Guest

    Danny J. Lesandrini wrote:
    > If not the VBScript RegExp, then do you have another suggestion for
    > testing strings to see if they match a particularly complex set of rules?


    You can use the LIKE operator
    (http://office.microsoft.com/en-us/assistance/HP010322531033.aspx) for
    basic pattern matching implemented at the engine level, which allows
    one to write quite powerful validation rules.

    Jamie.

    --
     
  11. DBG

    DBG
    Expand Collapse
    Guest

    Yeah, LIKE is a good option if you don't need to return and use substrings.
    If you need to match specific parts of a string using if elseif etc. the
    RegEx is probably better. Of course you could always just query the DB with
    multiple LIKE queries depenant on your DB load, number of users etc.

    -David

    "Jamie Collins" wrote:

    >
    > Danny J. Lesandrini wrote:
    > > If not the VBScript RegExp, then do you have another suggestion for
    > > testing strings to see if they match a particularly complex set of rules?

    >
    > You can use the LIKE operator
    > (http://office.microsoft.com/en-us/assistance/HP010322531033.aspx) for
    > basic pattern matching implemented at the engine level, which allows
    > one to write quite powerful validation rules.
    >
    > Jamie.
    >
    > --
    >
    >
     
  12. Jamie Collins

    Jamie Collins
    Expand Collapse
    Guest

    DBG wrote:
    > Of course you could always just query the DB with
    > multiple LIKE queries


    This approach can be better for code maintenance, using one LIKE for
    each validation rule (or search condition, etc). See this example:

    http://groups.google.com/group/microsoft.public.access/msg/19402d6a72c0d053

    Sure, they could probably have combined some rules but keeping them
    separate makes them easier to deduce, plus you get more meaningful
    failure messages.

    Jamie.

    --
     

Share This Page