C# Exercises

Home AgriMetSoft About Contact

REGular EXpression (Regex) in | C#

	    	
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Regular_Expression
{
  public partial class Form1 : Form
  {
    //https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html

    public Form1()
    {
      InitializeComponent();
    }

    private void Check_Email_Click(object sender, EventArgs e)
    {
      // mail@mail.com => ^([\w]+)@([\w]+)\.([\w]+)$
      System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^([\w]+)@([\w]+)\.([\w]+)$");
      if (regex.IsMatch(textBox1.Text))
        MessageBox.Show("True");
      else
        MessageBox.Show("False");
    }

    private void button1_Click(object sender, EventArgs e)
    {
      // Phone Number like : 0011 XXX XXX XXX => ^(0011)(([ ][0-9]{3}){3})$
      System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^(0011)(([ ][0-9]{3}){3})$");
      if (regex.IsMatch(textBox1.Text))
        MessageBox.Show("True");
      else
        MessageBox.Show("False");
    }

    /*
      //
      // http://www.google.com        => ^(http://www\.)([\w]+)\.([\w]+)$
      // Date XX/XX/XXXX           => ^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$
    */
  }
}

		
	 


Download the project of Visual Studio 2013 in DropBox Download


How to use Regex (REGular EXpression) in C# to find Email, Phone Number, Website


List of Exercises