2011-11-24 177 views
1

问题不仅在于从字符串中识别电子邮件地址。这是关于用公司电子邮件地址替换找到的电子邮件地址。如何从长字符串中识别电子邮件地址并将电子邮件地址替换为我公司的电子邮件地址?

例如,如果我有一个字符串如下:

“嗨,我的名字是约翰马丁。我是一个画家和雕塑家。如果你想购买我的画作,请看我的网站上的投资组合,然后与我联系约翰@ gmail.com

我想,以取代上面的字符串

”嗨,我的名字是约翰·马丁,我是一个画家和雕塑家。如果你想购买我的作品,请看看我的然后与我联系[email protected]

+2

它总是'john @ gmail.com'? –

+0

不..也可以是[email protected] .. :-) –

回答

0
public String GetEMailAddresses(string Input) 
{ 
    System.Text.RegularExpressions.MatchCollection MC = System.Text.RegularExpressions.Regex.Matches(Input, "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); 

    if(MC.Count > 0) 
     return MC[0].Value; 

    return ""; 
} 

您可以使用上述方法找到电子邮件地址。如果它返回的东西不是“”,这意味着它是一个电子邮件地址。现在,你可以简单地使用方法与string.replace与新的电子邮件地址,以取代旧

string input = "Hi, My name is John Martin. I am a painter and sculptor. If you wish to purchase my paintings please look at my portfolio on the site and then contact me on [email protected]"; 
      string email = GetEMailAddresses(input); 
      input = input.Replace(email, "[email protected]"); 
相关问题