C#正则表达式实例

private void okButton_Click(object sender, System.EventArgs e)
        
{
            
if(lastTextBox.Text==""|| firstTextBox.Text==""||this.addressTextBox.Text==""|| this.cityTextBox.Text==""||this.stateTextBox.Text==""|| this.zipTextBox.Text==""||this.phoneTextBox.Text=="")
            
{
                MessageBox.Show(
"请输入所有资料信息","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
this.lastTextBox.Focus();
                
return;
            }

            
if(!Regex.Match(this.lastTextBox.Text,@"^[A-Z][a-zA-Z]*$").Success)
            
{
                MessageBox.Show(
"重新输入名字","message",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
this.lastTextBox.Focus();
                
return;
            }

            
if(!Regex.Match(this.firstTextBox.Text,@"^[A-Z][a-zA-Z]*$").Success)
            
{
                MessageBox.Show(
"重输入姓","message",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
this.firstTextBox.Focus();
                
return;
            }

            
//if(!Regex.Match(this.addressTextBox.Text,@"^[0-9] + \s + ([a-zA-Z] +|[a-zA-Z] + \s[a-zA-Z]+)$").Success)
            if(!Regex.Match(this.addressTextBox.Text,@"^[a-zA-Z] +[a-zA-Z] + \s[a-zA-Z]$").Success)
            
            
{
                MessageBox.Show(
"invalid 重新输入地址","message",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
this.addressTextBox.Focus();
                
return;

            }

            
if(!Regex.Match(this.cityTextBox.Text,@"^([a-zA-Z]+|[a-zA-Z]+\s[a-zA-Z]+)$").Success)
            
{
                MessageBox.Show(
"invalid city 城市","message",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
this.cityTextBox.Focus();
                
return;
            }

            
if(!Regex.Match(this.stateTextBox.Text,@"^([a-zA-Z]+|[a-zA-Z]+\s[a-zA-Z]+)$").Success)
            
{
                MessageBox.Show(
"invalid state国家","message",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
this.cityTextBox.Focus();
                
return;
            }

            
if(!Regex.Match(this.zipTextBox.Text,@"^\d{6}$").Success)
            
{
                MessageBox.Show(
"invalid zip邮码","message",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
this.zipTextBox.Focus();
                    
return;
            }

            
if(!Regex.Match(this.phoneTextBox.Text,@"^[0-9]\d{3}-[1-9]\d{3}-\d{3,4}$").Success)
            
{
                MessageBox.Show(
"invalid phone电话号码","message",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
this.phoneTextBox.Focus();
                
return;
            }
  //[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+
            if(!Regex.Match(this.emailTextBox.Text,@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$").Success)  //  \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
            {
                MessageBox.Show(
"invalid email 邮箱","message",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
this.emailTextBox.Focus();
                
return;
            }
 //^[a-zA-z]+://匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$
            if(!Regex.Match(this.websiteTextBox.Text,@"^http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$").Success)  //http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
            {
                MessageBox.Show(
"invalid webSite 网址","message",MessageBoxButtons.OK,MessageBoxIcon.Error);
                
this.websiteTextBox.Focus();
                
return;
            }

            MessageBox.Show(
"thank you!","geoVi studio",MessageBoxButtons.OK,MessageBoxIcon.Information);
            Application.Exit();
//处理消息之后关闭所有的程序。
        }

posted @ 2006-02-28 18:12  ®Geovin Du Dream Park™  阅读(1115)  评论(2编辑  收藏  举报