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();//处理消息之后关闭所有的程序。
}
{
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();//处理消息之后关闭所有的程序。
}
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)