egex.IsMatch(str_day, @"^((0?[1-9])|((1|2)[0-9])|30|31)contentquot;);
}
9.数字输入
public bool IsNumber(string str_number)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_number, @"^[0-9]*contentquot;);
}
10. 密码长度 (6-18位)
public bool IsPasswLength(string str_Length)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_Length, @"^\d{6,18}contentquot;);
}
11. 非零的正整数
public bool IsIntNumber(string str_intNumber)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_intNumber, @"^\+?[1-9][0-9]*contentquot;);
}
三. 常用字符验证技巧
1. 大写字母
public bool IsUpChar(string str_UpChar)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_UpChar, @"^[A-Z]+contentquot;);
}
2. 小写字母
public bool IsLowChar(string str_UpChar)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_UpChar, @"^[a-z]+contentquot;);
}
3. 检查字符串重复出现的词
private void btnWord_Click(object sender, EventArgs e)
{
System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(label1.Text,
@"\b(?<word>\w+)\s+(\k<word>)\b", System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
if (matches.Count != 0)
{
foreach (System.Text.RegularExpressions.Match match in matches)
{
string word = match.Groups["word"].Value;
MessageBox.Show(word.ToString(),"英文单词");
}
}
else { MessageBox.Show("没有重复的单词"); }
}
4. 替换字符串
private void button1_Click(object sender, EventArgs e)
{
string strResult = System.Text.RegularExpressions.Regex.Replace(textBox1.Text, @"[A-Za-z]\*?", textBox2.Text);
MessageBox.Show("替换前字符:" + "\n" + textBox1.Text + "\n" + "替换的字符:" + "\n" + textBox2.Text + "\n" +
"替换后的字符:" + "\n" + strResult,"替换");
}
5. 拆分字符串
private void button1_Click(object sender, EventArgs e)
{
//实例:甲025-8343243乙0755-2228382丙029-32983298389289328932893289丁
foreach (string s in System.Text.RegularExpressions.Regex.Split(textBox1.Text,@"\d{3,4}-\d*"))
{
textBox2.Text+=s; //依次输出 "甲乙丙丁"
}
}
6. 验证输入字母
public bool IsLetter(string str_Letter)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_Letter, @"^[A-Za-z]+contentquot;);
}
7. 验证输入汉字
public bool IsChinese(string str_chinese)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_chinese, @"^[\u4e00-\u9fa5],{0,}contentquot;);
}
8. 验证输入字符串 (至少8个字符)
public
|