【问题标题】:Visual Basic Excel Regular Expression {}Visual Basic Excel 正则表达式 {}
【发布时间】:2015-10-30 16:45:01
【问题描述】:

我在使用 {} 时遇到了一些问题。当我得到像 {1,8} 这样的最大值时,它不起作用,我现在不知道为什么。最小值有效

Private Sub Highlvl_Expression()
Dim strPattern As String: strPattern = "[a-zA-Z0-9_]{1,8}"
Dim strReplace As String: strReplace = ""
Dim regEx As New RegExp
Dim Test As Boolean
With regEx
     .Global = True
     .MultiLine = True
     .IgnoreCase = False
     .Pattern = strPattern
End With
Test = regEx.Test(Highlvl.Value)
If regEx.Test(Highlvl.Value) Then
    MsgBox ("Validate")
Else
    MsgBox ("Not Validate")
End If
End Sub

【问题讨论】:

  • 使用锚点Dim strPattern As String: strPattern = "^[a-zA-Z0-9_]{1,8}$"

标签: regex excel vba


【解决方案1】:

您指定了在字符串中查找 1 到 8 个字母数字字符的模式。如果您对 9 个字符的字符串 "ABCDE6789" (regEx.Execute("ABCDE6789")) 运行正则表达式,您将有 2 个匹配项:ABCDE6789

如果要验证应包含最少或最多字符数的字符串,则需要使用anchors,即字符串断言^$ 的开始和结束。所以,使用

Dim strPattern As String: strPattern = "^[a-zA-Z0-9_]{1,8}$"

.Global = False

全局标志不是必需的,因为我们不是在寻找多个匹配项,而是寻找带有test 的单个真或假结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    相关资源
    最近更新 更多