0

我正在尝试匹配 xml 的标记值(音乐类型样式)。

这是一些例子:

一种音乐类型:(流行)(rel="tag">流行)

<span class="genres"><a href="http://www.mp3crank.com/genre/shoegaze" rel="tag">Pop</a></span>

两种音乐流派: (Reggae) (Ska) (rel="tag">Reggae) (rel="tag">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>

两种以上的音乐流派: (Alternative) (Indie) (Rock) (rel="tag">Alternative) (rel="tag">Indie) (rel="tag">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">部分的情况下获得“ Genre ”,那就更好了,但真的没关系。

这是我做的正则表达式,效果不好,即使存在两个或多个流派标签,也只匹配第一个标签。

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
4

1 回答 1

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
于 2013-05-22T19:08:50.497 回答