日期:2014-05-19  浏览次数:20687 次

两条小正则规则问题,解决即给分!
1.取出邮件地址:

string   a= "AA12_BB@163.com;BB21_CC@yahoo.com; "
a由许多邮件地址组成,每个地址由;隔开,现想取出第个邮件地址,
这样写不正确:
Regex   MyReg   =   new   Regex(@ "(\w)+\W(\w)+;{1} ");

2.查找某个字符在指定字符串中出现的次数

string   b= "3           *                 *                 *           Request   timed   out.
4           *                 *                 *           Request   timed   out.
    5           *                 *                 *           Request   timed   out. "

现想查找*的个数及Request   Timed   out个数

求解,解决给分



------解决方案--------------------
1题.试试
Regex MyReg = new Regex(@ "(\w)+\W(\w)+(\.(\w)+)+ ");
------解决方案--------------------
1
string a = "AA12_BB@163.com;BB21_CC@yahoo.com; ";
Text = Regex.Match(a, @ "(\w+@\w+(\.\w+)+); ").Result( "$1 "); //AA12_BB@163.com

2
string b = "....... ";
string key = "* ";
Console.WriteLine((b.Length - b.Replace(key, " ").Length) / key.Length);//9

key = "Request timed out ";
Console.WriteLine((b.Length - b.Replace(key, " ").Length) / key.Length);//3

------解决方案--------------------
string a = "AA12_BB@163.com;BB21_CC@yahoo.com; ";

Regex rg = new Regex(@ "(\w)+\W(\w)+\.(\w)+(?=;) ");

MatchCollection mc = rg.Matches(a);
foreach(Match m in mc)
{
Console.WriteLine(m.Value);

}
------解决方案--------------------
string b = "3 * * * Request timed out.4 * * * Request timed out.5 * * * Request timed out. ";

Regex rg2 = new Regex(@ "\* ");

MatchCollection mc2 = rg2.Matches(b);
Console.WriteLine(mc2.Count);

Regex rg3 = new Regex(@ " Request timed out ");

MatchCollection mc3 = rg3.Matches(b);
Console.WriteLine(mc3.Count);
------解决方案--------------------
^([_a-z0-9.]*)@([a-z0-9-]+(\.[a-z0-9-]+)*\.[a-z]+(?=;))
------解决方案--------------------
@ "(\w+(\.\w+)?@\w+(\.\w+)+); "
~~~~~~~~
------解决方案--------------------
根本不需要管;啊
------解决方案--------------------
\w+([-+. ']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
这个是邮件的正则,用这个去匹配应该就可以了