问题不仅在于从字符串中识别电子邮件地址。这是关于用公司电子邮件地址替换已找到的电子邮件地址。
就像。如果我有如下字符串:
“嗨,我叫约翰马丁。我是一位画家和雕塑家。如果你想购买我的画,请在网站上查看我的作品,然后在john@gmail.com”上与我联系。
我想将上面的字符串替换为
“嗨,我叫约翰马丁。我是一位画家和雕塑家。如果你想购买我的画,请在网站上查看我的作品,然后在support@companyname.com”上与我联系。
发布于 2011-11-24 06:45:27
下面的代码将替换与电子邮件id匹配的所有电子邮件字符串,Regex类可以在System.Text.RegularExpressions命名空间中找到
Regex emailReplace = new Regex(@"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}", RegexOptions.IgnoreCase);
emailReplace.Replace("YOUR TEXT HERE", "support@companyname.com");发布于 2011-11-24 06:36:52
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 support@companyname.com";
string email = GetEMailAddresses(input);
input = input.Replace(email, "support@companyname.com");https://stackoverflow.com/questions/8253014
复制相似问题