字符串的系列操作

/**
* @param regex
* 正则表达式字符串
* @param str
* 要匹配的字符串
* @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
*/
private static boolean match(String regex, String str) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}

// 3. 检查字符串重复出现的词
// 
// private void btnWord_Click(object sender, EventArgs e)
// {
// System.Text.RegularExpressions.MatchCollection matches =
// System.Text.RegularExpressions.Regex.Matches(label1.Text,
// 
// @"(?<word>w+)s+(k<word>)",
// 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("替换前字符:" + "
" + textBox1.Text + "
" + "替换的字符:" + "
"
// + textBox2.Text + "
" +
// 
// "替换后的字符:" + "
" + 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; //依次输出 "甲乙丙丁"
// }
// 
// }
原文地址:https://www.cnblogs.com/20gg-com/p/6037530.html