替换文本中的指定内容(电子邮件),以及其它指定的内容

本文介绍如何使用ASP.NET处理文本框中的输入,并通过正则表达式来提取电子邮件地址。同时展示了去除HTML标记的方法,适用于网页表单的数据预处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 <div>
        <asp:TextBox ID="TextBox1" runat="server" Width="519px"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Button" Width="65px"
            onclick="Button1_Click" /><br />
        <asp:TextBox ID="TextBox2" runat="server" Width="520px"></asp:TextBox>
    </div>

 

 

 protected void Button1_Click(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"([/w-]+(/./w+)*@([/w-]+/.)+/w{2,3})", RegexOptions.IgnoreCase); 正则根据自己的需求进行更改。
           string str= regex.Replace(TextBox1 .Text, ""); 将textBox1文本框中的电子邮件替换为"",替换内容可自定义
           Match match = regex.Match(TextBox1.Text);  //匹配文本框的中电子邮件
           TextBox2.Text = match .Groups [0].Value ;  //取出文本框中的电子邮件
        }

 

 

 MatchCollection match = regex.Matches(str);  //匹配多条
           
for (int i = 0; i < match.Count; i++)
            {
               
//循环读取match内容
            }

 

//去除HTML标记

public static string StripAllTags(string stringToStrip)
    {
        stringToStrip = Regex.Replace(stringToStrip, "</p(?://s*)>(?://s*)<p(?://s*)>", "/n/n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        stringToStrip = Regex.Replace(stringToStrip, "<br(?://s*)/>", "/n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        stringToStrip = Regex.Replace(stringToStrip, "/"", "''", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        stringToStrip = Regex.Replace(stringToStrip, "<[^>]+>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        stringToStrip = Regex.Replace(stringToStrip, "&[^;]+;", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);

        return stringToStrip;
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值