正则表达式 - 捕获以 8 位结尾的" and "之间的字符串,然后.xml



我正在使用Excel VBA。

我使用什么语法来查找一个位于双引号(不包括引号)之间并且以八位数后跟.xml结尾的字母数字字符串?

我认为数字和xml是由\d{8}(.xml)捕获的,但我不知道如何捕获导致八个数字的所有字符。

下面是一个示例字符串:"flws-20130929.xml"这是另一个:"aegy.ob-20731.xml"

谢谢你的帮助。

这里有一个只使用LIKE运算符而不使用VBScript Regexp对象的快速版本。

尝试以下操作:

s = "flws-20130929.xml"
If s Like "*########.xml" Then Msgbox "Yes"

你会看到它确实回来了。

如果您确实有双引号并希望排除它们:

s = """flws-20130929.xml"""
If s Like """*########.xml""" Then MsgBox Mid(s, 2, Len(s) - 2)

使用VBScript Regex 55

Sub GetLink()
Dim RegEx As Object
Set RegEx = New RegExp
Dim MatchCol As MatchCollection
With RegEx
    .Pattern = """([a-zA-Z.-]*[0-9]{8}.xml)"""
    .Global = True ' This parameter is very important or you will only get the first match within the tested string.
End With

' You would be getting that from the webpage, here I created a dummy
s = "<a href=""flws-20130929.xml""></a><p>A paragraph here with another link><a href=""flws-20120717.xml""></a></p>"
Debug.Print s ' (need the Immediate Window to see what it gives, CTRL + G)
Debug.Print RegEx.Test(s)
' Store the results in a collection of matches
Set MatchCol = RegEx.Execute(s)

If MatchCol.Count = 0 Then
    Debug.Print "No Match found"
Else
    For Each Match In MatchCol
        Debug.Print "source >>", Match.Value
        ' Showing you the submatches here. This is within the pattern, what is between parenthesis. Here you only have one anyway...
        For j = 0 To Match.SubMatches.Count - 1
            Debug.Print "[$" & j + 1 & "]", Match.SubMatches(j)
        Next j
    Next Match
End If

End Sub

在你的情况下,你会想做

For Each Match In MatchCol
    Debug.print Match.SubMatches(0)
Next Match

最新更新