Introduction

This code example uses regular expressions included in the namespace System.Text.RegularExpressions.Il form is composed of a RichTextBox control in which copies the contents of the file opened by the user through dialog unafinestra openfiledialog.Una After loading the file in the RichTextBox control, using a Button control Find the code that runs regular richiamal'espressione, which searches for email addresses, and these are then placed inside an iteration means ofa CheckedListBox foreach.Mediante a Button Save you can save all email addresses found previously by the use of regular expressions in a text file.

Building the Sample

The sample code was built with VisualStudio 2010 and intended to. NetFramework4
Description
I report here the complete code write with C # language, but also available to download in vb.net
 
C#
Modifica script|{#scriptcode_dlg.remove_script}
using System; 
using System.Collections.Genericusing System.ComponentModelusing System.Datausing System.Drawingusing System.Linqusing System.Textusing System.Windows.Formsusing System.IOusing System.Text.RegularExpressions; 
 
namespace Espressioni_Regolari 
{ 
    public partial class FrmMain : Form 
    { 
        public FrmMain() 
        { 
            InitializeComponent(); 
        }  
 
        // Evento Click del pulsante CaricaFileToolStripMenuItem privatevoid btnApriFile_Click(object sender, EventArgs e)  
        {  
            // Dichiaro un nuovo oggetto OpenFileDialog  
            OpenFileDialog open = new OpenFileDialog();  
            // Imposto la proprietà Filter in modo che possa aprire solo file di testo  
            open.Filter = "File di testo|*.txt";  
            // Se l'utente preme il tasto ok della finestra di dialogo if (open.ShowDialog() == DialogResult.OK)  
            {  
                // Leggo il contenuto del file aperto tramite la finestra di dialogo  
                StreamReader reader = new StreamReader(open.OpenFile());  
                // Scrivi all'interno di RichTextBox1 tutte le righe del File  
                RichTextBox1.Text = reader.ReadToEnd();  
                // Chiudo l'oggetto reader  
                reader.Close();  
            }  
        }  
 
 
        // Evento Click del pulsante SalvaIndirizziEmailToolStripMenuItem privatevoid btnSalvaIndirizziEmail_Click(object sender, EventArgs e)  
        {  
            // Dichiaro un nuovo oggetto SaveFileDialog  
            SaveFileDialog Save = new SaveFileDialog();  
            // Imposto la proprietà Filter in modo che possa salvare solo file di testo  
            Save.Filter = "File di testo|*.txt";  
            // Se l'utente preme il tasto ok della finestra di dialogo if (Save.ShowDialog() == DialogResult.OK)  
            {  
                // Scrivo il contenuto presente nella listbox lstEmail  
                StreamWriter Writer = new StreamWriter(Save.FileName);  
                // CheckedItems è una collezione in sola lettura che // restituisce tutti gli elementi spuntati foreach (string Item in lstEmail.CheckedItems)  
                // Salva gli indirizzi Email spuntati  
                Writer.WriteLine(Item);  
                // Chiudo l'oggetto Writer  
                Writer.Close();  
            }  
        }  
 
  // Evento Click del pulsante EsciToolStripMenuItem privatevoid btnEsci_Click(object sender, EventArgs e)  
        {  
            // Chiudo l'applicazione  
            Application.Exit();  
        }  
    } 
} 
This is a piece of code that searches for email addresses using regular expression, passing the variable text content of RichTextBox1.
Then using the Regex class is built for the regular expression will be used to scan and store email addresses in a CheckedListBox through the foreach iteration.
C#
Modifica script|{#scriptcode_dlg.remove_script}
       // Evento Click del pulsante TrovaIndirizziEmailToolStripMenuItem   
        private void btnTrovaIndirizziEmail_Click(object sender, EventArgs e)   
        {   
            // Dichiaro una variabile di tipo string ed assegno il contenuto di RichTextBox1   
            string text = RichTextBox1.Text;   
            // Tramite la Classe Regex genero un espressione regolare la quale estrapola tutti gli indirizzi Email dal testo   
            Regex Email = new Regex(@"\b(\w+)\s*(@|at|\[at\])\s*(\w+)\s*(\.|dot|\[dot\])(\w+)", RegexOptions.Multiline);   
            // Eseguo l'iterazione e creco tutte le corrispondenze dell'espressione regolare   
            foreach (Match M in Email.Matches(text))   
            {   
                // Aggiungo al listbox lstEmail tutti gli indirizzi Email ricavati dall'espressione regolare   
                lstEmail.Items.Add(String.Format("{0}@{1}{2}{3}", M.Groups[1], M.Groups[3], M.Groups[4].Value, M.Groups[5].Value), true);   
            }   
        } 
 

More Information

For more information http://community.visual-basic.it/carmelolamonica/