Thread: RegEx for two consecutive numbers (Any)

1. 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

2. 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>

3. 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. 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

5. 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.

6. 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
•