如何使用带访问权限的正则表达式匹配字符串 VBA
How can I matche an string using regex with access and VBA
我有这段代码,但是当我使用它时,UrlTest 变量 return 整个 URL 而不是我在正则表达式中定义的捕获组。 ^http:\/\/myanimelist\.net\/animelist\/(.*[^\/])(?:\/|)$
这是我的代码:
Private Function UrlTest(strUrl As String) As String
'Do the regEx for get the user in url
Dim regEx As New RegExp
regEx.Pattern = "^http:\/\/myanimelist\.net\/animelist\/(.*[^\/])(?:\/|)$"
regEx.IgnoreCase = True
regEx.Global = False
If regEx.Test(strUrl) Then
Dim matche As Object
Set matche = regEx.Execute(strUrl)
If matche.Count <> 0 Then
UrlTest = matche(0)
End If
Else
UrlTest = "false"
End If
End Function
此函数与 strUrl 和 http://myanimelist.net/animelist/example
的值 return 相同,而不是我想要的值:example
.
我无法理解!你可以看到,这个 Regex test 工作 !
UrlTest = matche(0).submatches(0)
你应该试试这个。
我有这段代码,但是当我使用它时,UrlTest 变量 return 整个 URL 而不是我在正则表达式中定义的捕获组。 ^http:\/\/myanimelist\.net\/animelist\/(.*[^\/])(?:\/|)$
这是我的代码:
Private Function UrlTest(strUrl As String) As String
'Do the regEx for get the user in url
Dim regEx As New RegExp
regEx.Pattern = "^http:\/\/myanimelist\.net\/animelist\/(.*[^\/])(?:\/|)$"
regEx.IgnoreCase = True
regEx.Global = False
If regEx.Test(strUrl) Then
Dim matche As Object
Set matche = regEx.Execute(strUrl)
If matche.Count <> 0 Then
UrlTest = matche(0)
End If
Else
UrlTest = "false"
End If
End Function
此函数与 strUrl 和 http://myanimelist.net/animelist/example
的值 return 相同,而不是我想要的值:example
.
我无法理解!你可以看到,这个 Regex test 工作 !
UrlTest = matche(0).submatches(0)
你应该试试这个。