c# 提取邮箱

提取邮箱数据的代码记录:

 

            string source = "1afdsa邮箱为:123@163.com 中国aa@bb.com.cna1中国人民";
            string regex = @"[a-zA-Z0-9\.\-_]+@[a-zA-Z0-9\-_]+[a-zA-Z0-9\.]+[a-zA-Z0-9\.]+";//字符串中@前面[a-zA-Z0-9\.\-_]+的意思是匹配@前面所有字母、数字、以及一些特殊符号
            MatchCollection mc = Regex.Matches(source, regex);//按照regex正规表达式到source字符串中提取邮箱并返回给mc
            for (int i = 0; i < mc.Count; i++)
            {
                Console.WriteLine("{0}->{1}", i, mc[i].Value);
            }

            var source = "1afdsa邮箱为:123@163.com 中国aa@bb.com.cna1中国人民";
            MatchCollection mcs = Regex.Matches(source, @"[-a-zA-Z0-9_.]+@[-a-zA-Z0-9]+(\.[a-zA-Z0-9]+){1,}");
            foreach (Match match in mcs)
            {
                var result = match.Value;
                Console.WriteLine(result);
            }

 

posted @ 2021-10-08 17:34  星星c#  阅读(261)  评论(0编辑  收藏  举报