c# - 如何使用 C# 中的正则表达式将文本中的电子邮件地址替换为姓名?

标签 c# .net regex

假设段落中的电子邮件地址现在是一个带有名称的字符串,如何替换它? 像[email protected] = xx , .com , .ae

输入=“联系[email protected][email protected]了解更多详情”

输出 =“联系 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 == "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="443c3c043d252c2b2b6a272b296a293d" rel="noreferrer noopener nofollow">[email protected]</a>"
    string name = match.Groups[1]; // "xx"
    string domain = match.Groups[2]; // "yahoo.com.my"
}

关于c# - 如何使用 C# 中的正则表达式将文本中的电子邮件地址替换为姓名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15611909/

相关文章:

.net - 使用 JSON API .Net Core 添加元数据中的总数

javascript - 提取十进制数列表

regex - Go 中糟糕的正则表达式

c# - 更新控件中的 asp.net 面板可见性

c# - Rhino Mocks - 使用 Arg.Matches

c# - ListView 'SelectionChanged' 事件后聚焦于 TextBox

c# - 将 pdfium.dll 添加到 visual studio

.net - 使用来自同一解决方案的自定义 MSBuild 任务?

c# - 升级.net框架后 Crystal 报表不工作

javascript - 正则表达式匹配所有具有两个或更多字符的html标签,但ul和ol以及其中的列表项