假设现在是一个字符串,我如何用名称替换段落中的电子邮件地址?像 xx@yahoo.com.my = xx , .com, .ae
输入 = "联系 abc@yahoo.com 或 defg@hotmail.eu 了解更多详情"
输出 = "联系 Abc 或 Defg 了解更多详情"
既然你要一个正则表达式,我会给你一个。
Regex regex = new Regex(@"(\.|[a-z]|[A-Z]|[0-9])*@(\.|[a-z]|[A-Z]|[0-9])*");
foreach (Match match in regex.Matches(inputString))
{
// match.Value == "xx@yahoo.com.my"
string name = match.Groups[1]; // "xx"
string domain = match.Groups[2]; // "yahoo.com.my"
}
int end = myString.IndexOf('@');
string name=myString.Substring(0, end);
像这样试试。
您可以在此处引用子字符串函数>>
Sting input = "contact abc@yahoo.com or defg@hotmail.eu for more details";
String pattern = @"(\S*)@\S*\.\S*";
String result = Regex.Replace(input , pattern , "$1");
public static string ReplaceEmail(string emailBody) {
string scrubbedemailBody = emailBody;
Regex regex = new Regex(@"(\.|[a-z]|[A-Z]|[0-9])*@(\.|[a-z]|[A-Z]|[0-9])*");
scrubbedemailBody = regex.Replace(scrubbedemailBody, match => {
return new string(' ', match.Length);
});
return scrubbedemailBody;
}