zoukankan      html  css  js  c++  java
  • 使用正则表达式和replace替换一个字符串中截取的一字符串


    aspx中:

    <table>
                    <tr>
                        <td style=" 178px" colspan="4">
                        <asp:Label id="Label1" runat="server" Text="输入要截取的字符串" CssClass="input_border1"></asp:Label>
                        </td>
                       
                    </tr>
                    <tr>
                       
                        <td colspan="2" >
       <asp:TextBox id="txtString" runat="server" CssClass="input_border1"></asp:TextBox></td>
                        <td style=" 621px" colspan="2">
       <asp:Button id="btnSubmit" runat="server" Text="截取" onclick="btnSubmit_Click" CssClass="btn_pic1"></asp:Button>&nbsp;
                        </td>
                    </tr>
                    <tr>
                      
                        <td style=" 203px" colspan="4">
       <asp:Label id="lblMessage" runat="server"></asp:Label></td>
                       
                    </tr>
                </table>

    aspx.cs中:
     public static Regex RX = new Regex(@"^-?[1-9]\d*|[[\u4e00-\u9fa5]+]$",RegexOptions.IgnoreCase);
      protected void btnSubmit_Click(object sender, System.EventArgs e)
      {
                this.lblMessage.Text = "";
                string str = this.txtString.Text;
                if (RX.IsMatch(str))
                {
                    int indexBegin = str.IndexOf("[");
                    int indexEnd = str.LastIndexOf("]");
                    string endstr = str.Substring(indexBegin + 1, indexEnd - indexBegin - 1);//截取“[ ]”里的文本
                    Regex Rx = new Regex(@"[\u4e00-\u9fa5]+", RegexOptions.IgnoreCase);
                    String resultStr = "<font color=red>" + endstr + "</font>";
                    string ResultAll = Rx.Replace(endstr, resultStr);
                    // lblMessage.Text = ResultAll;
                    Regex Rex = new Regex(@"[[\u4e00-\u9fa5]+]");
                    this.lblMessage.Text = Rex.Replace(str, "[" + ResultAll + "]");
                }
                else
                {
                    //return;
                    lblMessage.Text = str;
                }
      }

  • 相关阅读:
    通过SQL Server 2008数据库复制实现数据库同步备份
    SQL Server进制
    Server2008+SQL2008 日志读取代理器未运行 进程无法在“WINXXX”上执行“sp_replcmds”
    swing中使用皮肤包
    JTextArea的自动定位最后一行
    JFrame如何设置背景图片
    swing中单击回车相当于点击登录
    execute、executeUpdate、executeQuery三者的区别(及返回值)
    JTable设置透明
    在eclipse中导入android项目
  • 原文地址:https://www.cnblogs.com/zengwei/p/560260.html
Copyright © 2011-2022 走看看