我是正则表达式的新手,我正在尝试使用正则表达式验证我们数据库中的收据编号。
我们的收据可以采用以下格式:
- 0123456(Manditory 七位数,不多不少)
- 0126456a(强制七位数字和一个字母 az)
- 0126456ab(强制性七位数字,两个字母 az)
- 0126456abc(强制七位数字,三个字母 az)
我尝试过使用一堆不同的正则表达式组合,但似乎都不起作用。现在我有:
(\d)(\d)(\d)(\d)(\d)(\d)(\d)([a-z])?([a-z])?([a-z])?
但这允许超过 7 个数字,并且允许超过 3 个字母。
这是我在 Access 2010 中的 VBA 函数,它将验证表达式:
Function ValidateReceiptNumber(ByVal sReceipt As String) As Boolean
If (Len(sReceipt) = 0) Then
ValidateReceiptNumber = False
Exit Function
End If
Dim oRegularExpression As RegExp
' Sets the regular expression object
Set oRegularExpression = New RegExp
With oRegularExpression
' Sets the regular expression pattern
.Pattern = "(\d)(\d)(\d)(\d)(\d)(\d)(\d)([a-z])?([a-z])?([a-z])?"
' Ignores case
.IgnoreCase = True
' Test Receipt string
ValidateReceiptNumber = .Test(sReceipt)
End With
End Function