29/08/12 17:37
fabio7586
Salve, sto cercando di estrapolare deti dati da una pagina web, e per riuscirci ho utilizzato delle regular expression. Il problema è che ci sono risucito a metà, nel senso:
Il codice è corretto sino a dove ho lasciato in grassetto; le regex devono estrapolare le mail che sono subito dopo la scritta mailto: e che rispettino la sintassi, ed i nomi che sono gli unici tra i tag <h1>.
Il problema è che pur riuscendo ad ottenere il nome, non riesco a farlo uscire nella listbox...cosa che invece avviene per le mail.
Qualcuno è disponibile ad aiutarmi?
Imports System.IO Imports System.Text.RegularExpressions Public Class FrmMain Private Sub btnApriFile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnApriFile.Click Dim open As New OpenFileDialog() open.Filter = "File di testo|*.txt" If open.ShowDialog() = DialogResult.OK Then Dim reader As New StreamReader(open.OpenFile()) RichTextBox1.Text = reader.ReadToEnd() reader.Close() End If End Sub Private Sub btnSalvaIndirizziEmail_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSalvaIndirizziEmail.Click Dim Save As New SaveFileDialog() Save.Filter = "File di testo|*.txt" If Save.ShowDialog() = DialogResult.OK Then Dim Writer As New StreamWriter(Save.FileName) For Each Item As String In lstEmail.CheckedItems Writer.WriteLine(Item) Next Writer.Close() End If End Sub Private Sub btnTrovaIndirizziEmail_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnTrovaIndirizziEmail.Click Dim text As String = RichTextBox1.Text Dim startsearch As String = "<h1>" Dim endsearch As String = "</h1>" Dim Email As New Regex("mailto:" & "\b(\w+)\s*(@|at|\[at\])\s*(\w+)\s*(\.|dot|\[dot\])(\w+)", RegexOptions.Multiline) Dim testo As New Regex("(?<=" & startsearch & ").+(?=" & endsearch & ")", RegexOptions.Multiline) For Each M As Match In Email.Matches(text) lstEmail.Items.Add([String].Format("{0}@{1}{2}{3}", M.Groups(1), M.Groups(3), M.Groups(4).Value, M.Groups(5).Value), True) Next [b]For Each G As Match In testo.Matches(text) lstnome.Items.Add([String].Format("{0}{1}{2}{3}", G.Groups(1), G.Groups(2), G.Groups(3).Value, G.Groups(4).Value), True) Next[/b] End Sub Private Sub btnEsci_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnEsci.Click Application.[Exit]() End Sub End Class
Il codice è corretto sino a dove ho lasciato in grassetto; le regex devono estrapolare le mail che sono subito dopo la scritta mailto: e che rispettino la sintassi, ed i nomi che sono gli unici tra i tag <h1>.
Il problema è che pur riuscendo ad ottenere il nome, non riesco a farlo uscire nella listbox...cosa che invece avviene per le mail.
Qualcuno è disponibile ad aiutarmi?
aaa