正则-总结
最近在总结,这一年半以来,在这个公司,学习到了什么,看了一眼自己的博客,技术文章就写了几篇,惭愧 惭愧。
正则是一开始来公司时候学习的,感谢江同学,感谢大猫,江同学给的思想指导以及书籍,大猫童鞋说,会正则的女孩很强大。
可是,我的正则,仅仅学了个够用,皮毛。
总是这么个臭毛病,用到什么学什么,只要觉得够用,就再也不学了。老大说,我这个自我评价很中肯。
既然知道自己的不足,赶紧改吧。
我喜欢泛型List,数组相对来说,我用的比较少。贴点代码,望大家指导。
分析字符串里的有效信息所用到的正则。从字符串里,获得电话号码、Email、QQ、手机号码、固话等。
1 /// <summary>
2 /// 获取emali组
3 /// </summary>
4 /// <param name="content"></param>
5 /// <returns></returns>
6 public List<string> GetEmail(string content)
7 {
8 //Email
9 List<string> listemail = new List<string>();
10 Regex rxemail = new Regex(@"[a-zA-Z0-9]+([-+.]\w+)*@\w+([-.]\w+)*\.[com|cn|com.cn|net|org|org.cn|net.cn|cc|mobi]+");
11 MatchCollection matchEmail = rxemail.Matches(content);
12 for (int i = 0; i < matchEmail.Count; i++)
13 {
14 if (listemail.Contains(matchEmail[i].ToString()) == false)
15 {
16 listemail.Add(matchEmail[i].ToString());
17 }
18 }
19 return listemail;
20 }
21
22 /// <summary>
23 /// 获取手机组
24 /// </summary>
25 /// <param name="content"></param>
26 /// <returns></returns>
27 public List<string> GetTelePhone(string content)
28 {
29 //手机
30 List<string> listmobile = new List<string>();
31 Regex rxmobile = new Regex(@"1[35]\d{9}|18\d{9}");
32 MatchCollection matchMobile = rxmobile.Matches(content);
33 for (int i = 0; i < matchMobile.Count; i++)
34 {
35 if (listmobile.Contains(matchMobile[i].ToString()) == false)
36 {
37 listmobile.Add(matchMobile[i].ToString());
38 }
39 }
40 return listmobile;
41 }
42
43 /// <summary>
44 /// 获取电话号码组
45 /// </summary>
46 /// <param name="content"></param>
47 /// <returns></returns>
48 public List<string> GetPhone(string content)
49 {
50 //固定电话
51 List<string> listphone = new List<string>();
52 Regex rxtr = new Regex(@"\(0\d{2}\)[- ]?\d{8}|0\d{2}[- ]?\d{8}|\(0\d{3}\)[- ]?\d{7,8}|0\d{3}[- ]?\d{7,8}");
53 MatchCollection matchPhone = rxtr.Matches(content);
54 for (int i = 0; i < matchPhone.Count; i++)
55 {
56 if (listphone.Contains(matchPhone[i].ToString()) == false)
57 {
58 listphone.Add(matchPhone[i].ToString());
59 }
60 }
61 return listphone;
62 }
63
64 /// <summary>
65 /// 获取QQ组
66 /// </summary>
67 /// <param name="content"></param>
68 /// <returns></returns>
69 public List<string> GetQQ(string content)
70 {
71 List<string> listqq = new List<string>();
72 Regex rxQQ = new Regex(@"(?<=[Qq][Qq]|在线.*?)\b?[1-9]\d{4,9}\b?");
73 MatchCollection matcheQQ = rxQQ.Matches(content);
74 for (int i = 0; i < matcheQQ.Count; i++)
75 {
76 if (listqq.Contains(matcheQQ[i].ToString()) == false)
77 {
78 listqq.Add(matcheQQ[i].ToString());
79 }
80 }
81 return listqq;
82 }
83
84 /// <summary>
85 /// 获取msn组
86 /// </summary>
87 /// <param name="content"></param>
88 /// <returns></returns>
89 public List<string> GetMsn(string content)
90 {
91 List<string> listmsn = new List<string>();
92 Regex rxmsn = new Regex(@"(?:(?<=[Mm][Ss][Nn].*))[a-zA-Z0-9]+([-+.]\w+)*@\w+([-.]\w+)*\.[com|cn|com.cn|net|org|org.cn|net.cn|cc|mobi]+");
93 MatchCollection matchMSN = rxmsn.Matches(content);
94 for (int i = 0; i < matchMSN.Count; i++)
95 {
96 if (listmsn.Contains(matchMSN[i].ToString()) == false)
97 {
98 listmsn.Add(matchMSN[i].ToString());
99 }
100 }
101 return listmsn;
102 }
2 /// 获取emali组
3 /// </summary>
4 /// <param name="content"></param>
5 /// <returns></returns>
6 public List<string> GetEmail(string content)
7 {
8 //Email
9 List<string> listemail = new List<string>();
10 Regex rxemail = new Regex(@"[a-zA-Z0-9]+([-+.]\w+)*@\w+([-.]\w+)*\.[com|cn|com.cn|net|org|org.cn|net.cn|cc|mobi]+");
11 MatchCollection matchEmail = rxemail.Matches(content);
12 for (int i = 0; i < matchEmail.Count; i++)
13 {
14 if (listemail.Contains(matchEmail[i].ToString()) == false)
15 {
16 listemail.Add(matchEmail[i].ToString());
17 }
18 }
19 return listemail;
20 }
21
22 /// <summary>
23 /// 获取手机组
24 /// </summary>
25 /// <param name="content"></param>
26 /// <returns></returns>
27 public List<string> GetTelePhone(string content)
28 {
29 //手机
30 List<string> listmobile = new List<string>();
31 Regex rxmobile = new Regex(@"1[35]\d{9}|18\d{9}");
32 MatchCollection matchMobile = rxmobile.Matches(content);
33 for (int i = 0; i < matchMobile.Count; i++)
34 {
35 if (listmobile.Contains(matchMobile[i].ToString()) == false)
36 {
37 listmobile.Add(matchMobile[i].ToString());
38 }
39 }
40 return listmobile;
41 }
42
43 /// <summary>
44 /// 获取电话号码组
45 /// </summary>
46 /// <param name="content"></param>
47 /// <returns></returns>
48 public List<string> GetPhone(string content)
49 {
50 //固定电话
51 List<string> listphone = new List<string>();
52 Regex rxtr = new Regex(@"\(0\d{2}\)[- ]?\d{8}|0\d{2}[- ]?\d{8}|\(0\d{3}\)[- ]?\d{7,8}|0\d{3}[- ]?\d{7,8}");
53 MatchCollection matchPhone = rxtr.Matches(content);
54 for (int i = 0; i < matchPhone.Count; i++)
55 {
56 if (listphone.Contains(matchPhone[i].ToString()) == false)
57 {
58 listphone.Add(matchPhone[i].ToString());
59 }
60 }
61 return listphone;
62 }
63
64 /// <summary>
65 /// 获取QQ组
66 /// </summary>
67 /// <param name="content"></param>
68 /// <returns></returns>
69 public List<string> GetQQ(string content)
70 {
71 List<string> listqq = new List<string>();
72 Regex rxQQ = new Regex(@"(?<=[Qq][Qq]|在线.*?)\b?[1-9]\d{4,9}\b?");
73 MatchCollection matcheQQ = rxQQ.Matches(content);
74 for (int i = 0; i < matcheQQ.Count; i++)
75 {
76 if (listqq.Contains(matcheQQ[i].ToString()) == false)
77 {
78 listqq.Add(matcheQQ[i].ToString());
79 }
80 }
81 return listqq;
82 }
83
84 /// <summary>
85 /// 获取msn组
86 /// </summary>
87 /// <param name="content"></param>
88 /// <returns></returns>
89 public List<string> GetMsn(string content)
90 {
91 List<string> listmsn = new List<string>();
92 Regex rxmsn = new Regex(@"(?:(?<=[Mm][Ss][Nn].*))[a-zA-Z0-9]+([-+.]\w+)*@\w+([-.]\w+)*\.[com|cn|com.cn|net|org|org.cn|net.cn|cc|mobi]+");
93 MatchCollection matchMSN = rxmsn.Matches(content);
94 for (int i = 0; i < matchMSN.Count; i++)
95 {
96 if (listmsn.Contains(matchMSN[i].ToString()) == false)
97 {
98 listmsn.Add(matchMSN[i].ToString());
99 }
100 }
101 return listmsn;
102 }