我是新來的正則表達式,我試圖用正則表達式驗證數據庫中的收據號碼。7位數字後跟可選3個字母的正則表達式
我們的收入就可以派上以下格式:
- (Manditory七個數字,沒有更多,不會少)
- 0126456a(Manditory七個數字一個字母AZ)
- 0126456ab(Manditory七位數以兩個字母AZ)
- 0126456abc(Manditory七個數字用三個字母AZ)
我試過使用一堆不同的正則表達式組合,但似乎沒有任何工作。現在我有:
(\d)(\d)(\d)(\d)(\d)(\d)(\d)([a-z])?([a-z])?([a-z])?
但是,這是允許超過七位數字,並允許超過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