我正在尝试匹配xml的标记值(音乐类型样式)。
这是一些例子:
一种音乐类型:(流行音乐)(rel =“tag”> Pop)
<span class="genres"><a href="http://www.mp3crank.com/genre/shoegaze" rel="tag">Pop</a></span>
两种音乐类型:(雷鬼)(Ska)(rel =“tag”&gt; Reggae)(rel =“tag”&gt; Ska)
<span class="genres"><a href="http://www.mp3crank.com/genre/reggae" rel="tag">Reggae</a> / <a href="http://www.mp3crank.com/genre/ska" rel="tag">Ska</a></span>
超过两种音乐类型:(替代)(独立)(摇滚)(rel =“tag”&gt; Alternative)(rel =“tag”&gt; Indie)(rel =“tag”&gt; Rock)
<span class="genres"><a href="http://www.mp3crank.com/genre/alternative" rel="tag">Alternative</a> / <a href="http://www.mp3crank.com/genre/indie" rel="tag">Indie</a> / <a href="http://www.mp3crank.com/genre/rock" rel="tag">Rock</a></span>
我需要的是获取“类型”值以将其附加到变量中:
rel="tag">Genre</a>
...或更好,如果我可以在没有 rel =“tag”&gt;的情况下获得“流派” 部分,但真的无所谓。
这是我做过的RegEx,效果不好,即使存在两个或多个流派标签,也只匹配第一个标签。
Dim RegEx_AlbumStyle As New Regex(<a><![CDATA[rel=.+</a>\s?[^><]|rel=.+</a>]]></a>.Value)
这是代码:
Dim AlbumStyle as string
Dim RegEx_AlbumStyle As New Regex(<a><![CDATA[rel=.+</a>\s?[^><]|rel=.+</a>]]></a>.Value)
If Line.Contains(<a><![CDATA[<span class="genres">]]></a>.Value) Then
For Each Style In RegEx_AlbumStyle.Match(Line).Groups
MsgBox("match:" & Style.ToString)
' I need to append all found matches to a string variable
' AlbumStyle += ", " & Style.ToString
' But I only find one match even if exists more than one genre value in the string
Next
End If
答案 0 :(得分:1)
同意这可能在将来破裂并且不是最好的方式,但如果您想要走这条路线,这可能会有所帮助。假设span标记已加载到字符串中,这将为我返回3个消息框:
Private Sub Input()
Dim genreString As String = "<span class=""genres""><a href=""http://www.mp3crank.com/genre/alternative"" rel=""tag"">Alternative</a> / <a href=""http://www.mp3crank.com/genre/indie"" rel=""tag"">Indie</a> / <a href=""http://www.mp3crank.com/genre/rock"" rel=""tag"">Rock</a></span>"
ShowGenres(genreString)
End Sub
Private Function ShowGenres(ByVal s As String) As String
Dim m As Match = Regex.Match(s, "tag"">(\w+)<")
Do While m.Success
MessageBox.Show(m.Groups(1).ToString)
m = m.NextMatch()
Loop
Return False
End Function