2

假设现在是一个字符串,我如何用名称替换段落中的电子邮件地址?像 xx@yahoo.com.my = xx , .com, .ae

输入 = "联系 abc@yahoo.com 或 defg@hotmail.eu 了解更多详情"

输出 = "联系 Abc 或 Defg 了解更多详情"

4

4 回答 4

2

既然你要一个正则表达式,我会给你一个。

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"
}
于 2013-03-25T10:04:40.237 回答
1
int end = myString.IndexOf('@');

string name=myString.Substring(0, end);

像这样试试。

您可以在此处引用子字符串函数>>

http://www.dotnetperls.com/substring

于 2013-03-25T09:56:20.483 回答
0
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");
于 2013-03-25T10:04:23.643 回答
0
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;
    }
于 2017-04-27T20:55:08.553 回答