搜索文件中的email
System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\11.txt",System.Text.Encoding.GetEncoding("gb2312"));
string str = sr.ReadToEnd();
System.Text.RegularExpressions.Regex mailRegex = new System.Text.RegularExpressions.Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");
System.Text.RegularExpressions.MatchCollection mc = mailRegex.Matches(str);
for(int i = 0; i<mc.Count; i++ )
Console.WriteLine( mc[i].ToString() );
文件里单单一个email到是能读出来
前后加写其他东西,就读不出来了
???????

解决方案 »

  1.   

    用beibeilong(whylove)的思路,如果有多个邮件地址并有;或其它分割符,可以用Split分离到一个数组,然后循环。
      

  2.   

    using System.Text.RegularExpression;
    using System.Text.Encoding;StreamReader sr = new StreamReader(yourFilePath, Encoding.Default);
    string temp = sr.ReadToEnd();
    MatchCollection mc = Regex.Match(temp, "\\b[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z0-9._%-]{2,4}\\b");
    foreach(Match m in mc)
    {
        string emailStr = m.Value;
    }
      

  3.   

    \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
      

  4.   

    System.Text.RegularExpressions.Regex mailRegex = new System.Text.RegularExpressions.Regex(@"[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+");
    去掉开始跟结束符号匹配,要不然就只能匹配一个email地址了。
      

  5.   

    用正则表达式:1. Expression:  ^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-z
    A-Z]\.)+[a-zA-Z]{2,9})$
        
    Description:   regex to validate email address noteworthy: (1) It allows usernames with 1 or 2 alphanum characters, or 3+ chars can have -._ in the middle. username may NOT start/end with -._ or any other non alphanumeric character. (2) It allows heirarchical ...  Matches:  [[email protected]], [[email protected]], [[email protected]]  [ More Details]  Non-Matches:  [[email protected]], [[email protected]], [[email protected]]  2. 
    Expression:  ^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Z
    a-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-z
    A-Z]{2,6}$
        
    Description:   The most complete email validation routine I could come up with. It verifies that: - Only letters, numbers and email acceptable symbols (+, _, -, .) are allowed - No two different symbols may follow each other - Cannot begin with a symbol - Ending domain ...  Matches:  [[email protected]], [[email protected]], [[email protected]]  [ More Details]  Non-Matches:  [[email protected]], [[email protected]], [[email protected]]  3
    Expression:  ^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[
    ^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA
    -Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01
    -\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?
    (?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-
    zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angl
    e)>)$
        
    Description:   This accepts RFC 2822 email addresses in the form: [email protected] OR Blah <[email protected]> RFC 2822 email 'mailbox': mailbox = name-addr | addr-spec name-addr = [display-name] "<" addr-spec ">" addr-spec = local-pa ...  Matches:  [[email protected]], [Name Surname <[email protected]>], ["b. blah"@blah.co.nz]  [ More Details]  Non-Matches:  [name [email protected]], [name."surname"@blah.com], [[email protected]]我觉得是这样的,你的每一个email肯定不可以和其他的字符连在一起,至少有一个空格或者说逗号分割,如果是空格,你可以在正则串的开头和结尾再加一个空格。这样应该可以吧。
      

  6.   

    jimh(jimmy)的方法试了
    ok了!!!!!!!!!!!!
    感谢ing!!!!!!!!!!!!!!