1、 IsMatch()方法,IsMatch()方法实际上是一个返回Bool值得方法,如果测试字符满足正则表达式返回True否则返回False。
输出:
原文链接:https://www.f2er.com/regex/362182.html
例子:
//匹配的正则表达式,去掉@不影响效果 Regex r = new Regex(@"^[0-9]"); //开始匹配 Match m = r.Match(this.textBox1.Text); while (m.Success) { MessageBox.Show("首位是数字"); return; }
检测
textBox1中输入的值,首位是不是数字。
小注:
例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace TestRegularExpressions { class Program { static void Main(string[] args) { string RegularText = "\\w{1,}@\\w{1,}\\."; string groupEmail = "111@126.com"; if (Regex.IsMatch(groupEmail,RegularText)) { Console.WriteLine(Regex.Replace(groupEmail,"@","===")); } else { Console.WriteLine("未匹配成功!"); } Console.ReadKey(); } } }
例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace TestRegularExpressions { class Program { static void Main(string[] args) { string RegularText = ";"; string groupEmail = "111@126.com;222@126.com;333@126.com;444@126.com;"; string[] Email; Email = Regex.Split(groupEmail,RegularText); foreach (string personEmail in Email) { Console.WriteLine(personEmail); } Console.ReadKey(); } } }
输出:
小注:
Split函数小封装:
#region 根据pattern拆分字符串 /// <summary> /// 根据pattern拆分字符串 /// </summary> /// <param name="input">待拆分的字符串</param> /// <param name="pattern">拆分标识符</param> /// <returns>拆分后数组</returns> private string[] SplitString(string input,string pattern) { string[] Email; Email = Regex.Split(input,pattern); return Email; } #endregion