Results 1 to 6 of 6
  1. #1
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    RegEx for two consecutive numbers (Any)

    Is it possible to create a regular expression that validates two consecutive two-digit numbers:
    Valid:
    00/01
    06/07
    29/30

    Invalid:
    01/03

    Probably not a good place to post it, but just on the off-chance that someone knows! TIA --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  2. #2
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: RegEx for two consecutive numbers (Any)

    Sam
    Does the following satisfy your needs?

    <div style="width: 100%; background-color: #FFFFFF;"><code><font color=black><font color=blue>Function</font color=blue> IsConsecNos(strText <font color=blue>As</font color=blue> <font color=blue>String</font color=blue>) <font color=blue>As</font color=blue> <font color=blue>Boolean</font color=blue>
    <font color=blue>Dim</font color=blue> i <font color=blue>As</font color=blue> <font color=blue>Long</font color=blue>
    <font color=blue>Dim</font color=blue> AscChr <font color=blue>As</font color=blue> <font color=blue>Long</font color=blue>
    <font color=blue>Dim</font color=blue> InitVal, FinVal As Long
    <font color=blue>For</font color=blue> i = 1 <font color=blue>To</font color=blue> Len(strText) - 2
    AscChr = Asc(Right(strText, Len(strText) - i))
    <font color=blue>If</font color=blue> AscChr < 48 <font color=blue>Or</font color=blue> AscChr > 57 <font color=blue>Then</font color=blue>
    InitVal = Val(Left(strText, AscChr - 1))
    FinVal = Val(Right(strText, Len(strText) - 1 - i))
    <font color=blue>If</font color=blue> FinVal - InitVal = 1 <font color=blue>Then</font color=blue> IsConsecNos = <font color=blue>True</font color=blue>
    <font color=blue>Exit</font color=blue> <font color=blue>Function</font color=blue>
    <font color=blue>End</font color=blue> <font color=blue>If</font color=blue>
    <font color=blue>Next</font color=blue> i
    <font color=blue>End</font color=blue> <font color=blue>Function</font color=blue></font color=black></code></div hiblock>
    Regards
    Don

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: RegEx for two consecutive numbers (Any)

    Or this:

    Function ValidateString(strText As String) As Boolean
    Dim intPos As Integer
    Dim lngVal1 As Long
    Dim lngVal2 As Long
    intPos = InStr(strText, "/")
    If intPos > 0 Then
    lngVal1 = Val(Left(strText, intPos - 1))
    lngVal2 = Val(Mid(strText, intPos + 1))
    ValidateString = (lngVal2 - lngVal1 = 1)
    End If
    End Function

  4. #4
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: RegEx for two consecutive numbers (Any)

    Not using RegExp; the pattern would be harder to build than a function.

    If you meant one at a time:

    Function boolConsecNumTest(strIn As String, strSep As String) As Boolean
    On Error Resume Next
    boolConsecNumTest = (CLng(Split(strIn, strSep)(0)) = CLng(Split(strIn, strSep)(1)) - 1)
    End Function
    -John ... I float in liquid gardens
    UTC -7ąDS

  5. #5
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: RegEx for two consecutive numbers (Any)

    No, no, no. I knew that I would get in trouble posting here. Cannot write code: must be RegEx. It's actually for a XSD schema, so I only have RegEx.
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: RegEx for two consecutive numbers (Any)

    Sorry, can't help then.

Posting Permissions

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