vba 7 位数字后跟可选的 3 个字母的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14792063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:33:35  来源:igfitidea点击:

Regular expression for 7 digits followed by an optional 3 letters

regexvbams-access

提问by Lil' Bits

I'm new to regular expressions, and I'm trying to validate receipt numbers in our database with a regular expression.

我是正则表达式的新手,我正在尝试使用正则表达式验证我们数据库中的收据编号。

Our receipts can come in the following formats:

我们的收据可以采用以下格式:

  • 0123456 (Manditory seven digits, no more, no less)
  • 0126456a (Manditory seven digits with one letter a-z)
  • 0126456ab (Manditory seven digits with two letters a-z)
  • 0126456abc (Manditory seven digits with three letters a-z)
  • 0123456(强制七位数,不多不少)
  • 0126456a(强制七位数字加一个字母az)
  • 0126456ab(强制七位数字加两个字母az)
  • 0126456abc(强制七位数字加三个字母az)

I've tried using a bunch of different regex combinations, but none seem to work. Right now I have:

我试过使用一堆不同的正则表达式组合,但似乎都不起作用。现在我有:

(\d)(\d)(\d)(\d)(\d)(\d)(\d)([a-z])?([a-z])?([a-z])?

But this is allowing for more than seven digits, and is allowing more than 3 letters.

但这允许超过 7 个数字,并且允许超过 3 个字母。

Here is my VBA function within Access 2010 that will validate the expression:

这是我在 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

回答by Rohit Jain

You probably need to use anchors at the ends. Further your regex can be simplified to: -

您可能需要在末端使用锚点。此外,您的正则表达式可以简化为:-

^\d{7}[a-z]{0,3}$
  • \d{7}matches exactly 7 digits. You don't need to use \d7 times for that.
  • {0,3}creates a range, and matches 0 to 3 repetition of preceding pattern,
  • Caret(^)matches the start of the line
  • Dollar($)matches the end of the line.
  • \d{7}完全匹配7 digits。您不需要为此使用\d7 次。
  • {0,3}创建一个范围,并匹配前面模式的 0 到 3 个重复,
  • Caret(^)匹配行的开头
  • Dollar($)匹配行尾。

回答by Akshay Joshi

^(\d){7}[a-z]{0,3}$might work well. The ^and $will match the start and end of line respectively.

^(\d){7}[a-z]{0,3}$可能工作得很好。在^$将分别匹配行的开头和结尾。

回答by Kendall Frey

You may want to make sure you are matching the entire string, by using anchors.

您可能希望通过使用锚点来确保匹配整个字符串。

^(\d)(\d)(\d)(\d)(\d)(\d)(\d)([a-z])?([a-z])?([a-z])?$

You can also simplify the regex. First, you don't need all those parentheses.

您还可以简化正则表达式。首先,您不需要所有这些括号。

^\d\d\d\d\d\d\d[a-z]?[a-z]?[a-z]?$

Also, you can use limited repetition, to prevent repeating yourself.

此外,您可以使用有限的重复,以防止重复自己。

^\d{7}[a-z]{0,3}$

Where {7}means 'exactly 7 times', and {0,3}means '0-3 times'.

其中{7}表示“正好 7 次”,{0,3}表示“0-3 次”。