用C#通过正则表达式截取字符串中符合条件的子字符串
仅仅作为简单的记录,不多说直接上代码(仅测试使用):
private void Test() { Regex ConnoteA = new Regex("^[a-zA-Z]\\d{8}$"); Regex ConnoteAA = new Regex("^[a-zA-Z]{2}\\d{7,10}$"); Regex ConnoteAAA = new Regex("^[a-zA-Z]{3}\\d{5,9}$"); Regex ConnoteAAAA = new Regex("^[a-zA-Z]{4}[a-zA-Z0-9]{8}$"); string haha = "w11111111"; string xixi = "ww1111111"; string hehe = "w111111111"; string lala = "W11111111"; string longlongstring = "w11111111222q33333333555555ee1234567890yyy111111111yyy000000AK123456Me123456tttt12345678"; bool result = ConnoteA.IsMatch(haha); result = ConnoteA.IsMatch(xixi); result = ConnoteA.IsMatch(hehe); result = ConnoteA.IsMatch(lala); foreach (Match mch in ConnoteA.Matches(longlongstring)) { string x = mch.Value.Trim(); } foreach (Match mch in Regex.Matches(longlongstring, "([a-zA-Z]\\d{8})|([a-zA-Z]{2}\\d{7,10})|([a-zA-Z]{3}\\d{5,9})|([a-zA-Z]{4}[a-zA-Z0-9]{8})|((AK|AL|BU|CR|EL|HF|HU|SW|SY|WU|ME)\\d{6})|([a-zA-Z]{4}\\d{8}[A-Za-z0-9]{3}\\d{4})")) { string x = mch.Value.Trim(); } }
更多正则表达式的信息,请看如下链接:http://fineboy.cnblogs.com/archive/2005/09/08/232410.html