Código
#Region " RegEx Matches To List " ' [ RegEx Matches To List Function ] ' ' // By Elektro H@cker ' ' Examples : ' Dim str 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>" ' For Each match In RegEx_Matches_To_List(str, <a><![CDATA[tag">(\w+)<]]></a>.Value) : MsgBox(match) : Next Private Function RegEx_Matches_To_List(ByVal str As String, ByVal RegEx_Pattern As String) As List(Of String) Dim match As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(str, RegEx_Pattern) Dim Match_List As New List(Of String) Do While match.Success Match_List.Add(match.Groups(1).ToString) match = match.NextMatch() Application.DoEvents() Loop Return Match_List End Function #End Region
Unas cuantas expresiones regulares que he escrito para facilitar algunas taréas:
Código
' Dim str As String = <a><![CDATA[<href="http://www.mp3crank.com/feed"]]></a>.Value ' MsgBox(Match_RegEx_MainBase_Url(Str)) ' Result: http://www.mp3crank.com Private Function Match_RegEx_MainBase_Url(ByVal str As String) As String ' Match criteria: ' ' http://url.domain ' https://url.domain ' www.url.domain Dim RegEx As New System.Text.RegularExpressions.Regex( _ <a><![CDATA[(http://|https://|www).+\.[0-9A-z]]]></a>.Value) Return RegEx.Match(str).Groups(0).ToString End Function
Código
' Dim str As String = <a><![CDATA[<href="http://www.mp3crank.com/feed"]]></a>.Value ' MsgBox(Match_RegEx_Url(str)) ' Result: http://www.mp3crank.com/feed Private Function Match_RegEx_Url(ByVal str As String) As String ' Match criteria: ' ' http://url ' https://url ' www.url Dim RegEx As New System.Text.RegularExpressions.Regex( _ <a><![CDATA[(http://|https://|www).+\b]]></a>.Value) Return RegEx.Match(str).Groups(0).ToString End Function
Código
' Dim str As String = <a><![CDATA[href="http://www.mp3crank.com/the-rolling-stones/deluxe-edition.htm"]]></a>.Value ' MsgBox(Match_RegEx_htm_html(str)) ' Result: http://www.mp3crank.com/the-rolling-stones/deluxe-edition.htm Private Function Match_RegEx_htm_html(ByVal str As String) As String ' Match criteria: ' ' http://Text.htm ' http://Text.html ' https://Text.htm ' https://Text.html ' www.Text.htm ' www.Text.html Dim RegEx As New System.Text.RegularExpressions.Regex( _ <a><![CDATA[(http://|https://|www).*\.html?]]></a>.Value) Return RegEx.Match(str).Groups(0).ToString End Function
Código
' Dim str As String = <a><![CDATA[href=>Drifter - In Search of Something More [EP] (2013)</a>]]></a>.Value ' MsgBox(Match_RegEx_Tag(str)) ' Result: Drifter - In Search of Something More [EP] (2013) Private Function Match_RegEx_Tag(ByVal str As String) As String ' Match criteria: ' ' >..Text..< Dim RegEx As New System.Text.RegularExpressions.Regex( _ <a><![CDATA[>([^<]+?)<]]></a>.Value) Return RegEx.Match(str).Groups(1).ToString End Function