[AccessD] OT Regular Expressions

MartyConnelly martyconnelly at shaw.ca
Tue May 27 18:13:26 CDT 2003



Martin Reid wrote:

> [a-zA-Z]+\w*\d+\w*"
>
> What does the above regular expression do???
>
>
>
> Martin

 Here is a Regular Expression documentor I got permission to use a few 
years back
from Don Edwards.

gives you
any of
Followed by a, -, z, A, -, Z one or more times
Followed by word-char zero or more times
Followed by digit one or more times
Followed by word-char zero or more times


'  Attribute VB_Name = "mDocumenter"
' 'From: Don Edwards <don.edwards at ci.seattle.wa.us>
' see regexpressions at www.vbxml.com or www.topxml.com

Public Const TestExpr As String = _
        "^[\w\-]+(\.[\w\-]{2,})*@[\w\-]+(\.[\w\-]{2,})*$"
Public gtxtPatternDescription As String
Private bForceLine As Boolean
Private bNewline As Boolean
Private bIndent As Boolean
Private bFollowed As Boolean
Private lM As Long
Private lH As Long
Private lPattern As Long
Private sIn As String, sOut As String
'From: Don Edwards <don.edwards at ci.seattle.wa.us>

Sub TestDocReg()
'Debug.Print RegExpD("^[\w\-]+(\.[\w\-]{2,})*@[\w\-]+(\.[\w\-]{2,})*$")
'Debug.Print 
RegExpD("^[01]?\s*[\(\.-]?(\d{3})[\)\.-]?\s*(\d{3})[\.-](\d{4})$")
Debug.Print RegExpD("/.//created/@month")
Debug.Print RegExpD("@* | * | pi() | comment() | text()")
End Sub

Private Sub NewLine(ByVal lIndent As Long)
Static lLineStart As Long
    If sOut = "" Then Exit Sub
    If lLineStart > Len(sOut) Or lLineStart = 0 Then lLineStart = 1
    If Trim(Mid(sOut, lLineStart)) = "" Then Exit Sub
    If bNewline Then sOut = sOut & vbCrLf
    If bIndent And (lIndent > 0) Then sOut = sOut & Space(lIndent)
    If bFollowed Then sOut = sOut & "Followed by"
   
    lLineStart = Len(sOut) + 1
   
    bForceLine = False
End Sub

Private Sub AddOn(ByVal sAdd As String, _
    ByVal lIndent As Long, Optional ByVal pbForceLine As Boolean = False)
   
    If bForceLine Or pbForceLine Then NewLine lIndent
   
    If Len(sOut) = 0 And Left(sAdd, 1) = " " Then
        sAdd = Mid(sAdd, 2)
    ElseIf Right(sOut, 1) = " " And Left(sAdd, 1) = " " Then
        sAdd = Mid(sAdd, 2)
    ElseIf Len(sOut) >= Len(vbCrLf) And Left(sAdd, 1) = " " Then
        If Right(sOut, Len(vbCrLf)) = vbCrLf Then
            sAdd = Mid(sAdd, 2)
        End If
    End If
   
    sOut = sOut & sAdd
       
    If Len(sAdd) < Len(vbCrLf) Then
        bNewline = True
    Else
        bNewline = (Right(sAdd, Len(vbCrLf)) <> vbCrLf)
    End If
    bNewline = True
    bIndent = True
    bFollowed = True
End Sub

Public Function RegExpD(ByVal rexIn As String) As String
    lH = 1
    lM = Len(rexIn)
    lPattern = 0
    sIn = rexIn
    sOut = ""
    bNewline = True
    bFollowed = False
    bIndent = True
    Debug.Print lH, lM
    Do
        RegExpD2 0
    Loop Until lH > lM
    RegExpD = sOut
End Function

Public Sub RegExpD2(ByVal lIndent As Long)
    Dim lP As Long
    Do Until lH > lM
        Select Case Mid(sIn, lH, 1)
        Case "^":
            AddOn " Start of input", lIndent
            lH = lH + 1
            bForceLine = True
            bNewline = True
        Case "$":
            AddOn " End of input", lIndent
            lH = lH + 1
            bForceLine = True
            bNewline = True
        Case "*":
            bForceLine = False
            AddOn " zero or more times", lIndent
            lH = lH + 1
            bForceLine = True
            bNewline = True
        Case "+":
            bForceLine = False
            AddOn " one or more times", lIndent
            lH = lH + 1
            bForceLine = True
            bNewline = True
        Case "?":
            bForceLine = False
            AddOn " zero or one times", lIndent
            lH = lH + 1
            bForceLine = True
            bNewline = True
        Case ".":
            AddOn " any non-newline", lIndent
            lH = lH + 1
            bNewline = True
        Case "(":
            lPattern = lPattern + 1
            lP = lPattern
            lH = lH + 1
            AddOn " Pattern-" & CStr(lPattern) & ": (", True
            bForceLine = True
            bFollowed = False
           
            RegExpD2 lIndent + 4
           
            bFollowed = False
            AddOn ")-end-Pattern-" & CStr(lP), lIndent, True
            bForceLine = True
            bNewline = True
        Case ")":
            lH = lH + 1
            Exit Sub
        Case "|":
            lH = lH + 1
            AddOn " or", lIndent
            bNewline = True
        Case "{":
            bForceLine = False
            lH = lH + 1
            AddOn " ", lIndent
            Do
                If lH > lM Then Exit Do
                Select Case Mid(sIn, lH, 1)
                Case "}":
                    AddOn " times", lIndent
                    lH = lH + 1
                    Exit Do
                Case ",":
                    lH = lH + 1
                    If Mid(sIn, lH, 1) = "}" Then
                        AddOn " or more", lIndent
                    Else
                        AddOn " to ", lIndent
                    End If
                Case Else:
                    AddOn Mid(sIn, lH, 1), lIndent
                    lH = lH + 1
                End Select
            Loop
            bForceLine = True
            bNewline = True
        Case "[":
            lH = lH + 1
            If Mid(sIn, lH, 1) = "^" Then
                AddOn " any except", lIndent, True
            Else
                AddOn " any of", lIndent, True
            End If
            lP = lH
            Do
                If lH > lM Then Exit Do
                If lH <> lP Then
                    AddOn ",", lIndent
                End If
                ParseChar lIndent
            Loop Until Mid(sIn, lH, 1) = "]"
            lH = lH + 1
            bForceLine = True
            bNewline = True
        Case "/":
            lH = lH + 1
            AddOn " Char-", lIndent, True
            Do
                If lH > lM Then Exit Do
                If Mid(sIn, lH, 1) = "/" Then Exit Do
                AddOn Mid(sIn, lH, 1), lIndent
                lH = lH + 1
            Loop
            lH = lH + 1
            bNewline = True
        Case "\":
            ParseChar lIndent
            bNewline = True
        Case " ": AddOn " space", lIndent
            lH = lH + 1
            bNewline = True
        Case ",": AddOn " comma", lIndent
            lH = lH + 1
            bNewline = True
        Case Else: AddOn " " & Mid(sIn, lH, 1), lIndent
            lH = lH + 1
            bNewline = True
        End Select
    Loop
End Sub

Private Sub ParseChar(ByVal lIndent As Long)
    Select Case Mid(sIn, lH, 1)
    Case "\":
        lH = lH + 1
        Select Case Mid(sIn, lH, 1)
        Case "b": AddOn " word-edge", lIndent
        Case "B": AddOn " not-word-edge", lIndent
        Case "d": AddOn " digit", lIndent
        Case "D": AddOn " not-digit", lIndent
        Case "f": AddOn " formfeed", lIndent
        Case "n": AddOn " newline", lIndent
        Case "r": AddOn " return", lIndent
        Case "s": AddOn " whitespace", lIndent
        Case "S": AddOn " non-whitespace", lIndent
        Case "t": AddOn " tab", lIndent
        Case "v": AddOn " vertical-tab", lIndent
        Case "w": AddOn " word-char", lIndent
        Case "W": AddOn " not-word-char", lIndent
        Case "1" To "9": AddOn " Pattern-", lIndent
            Do
                AddOn Mid(sIn, lH, 1), lIndent
                lH = lH + 1
                If lH > lM Then Exit Do
                If InStr("0123456789", Mid(sIn, lH, 1)) = 0 Then Exit Do
            Loop
            lH = lH - 1
        Case Else:
            AddOn " " & Mid(sIn, lH, 1), lIndent
        End Select
        lH = lH + 1
    Case " ": AddOn " space", lIndent
        lH = lH + 1
    Case ",": AddOn " comma", lIndent
        lH = lH + 1
    Case Else: AddOn " " & Mid(sIn, lH, 1), lIndent
        lH = lH + 1
    End Select
End Sub





More information about the AccessD mailing list