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;
                }
      }

  • 相关阅读:
    socket使用大全
    UIImageView控件使用(转)
    多线程,socket,http,asihttpRequest,等总结集合
    ios 如何判断字符串含有中文字符?
    修改UISearchBar
    abc222_e Red and Blue Tree(树上差分+01背包)
    2020icpc上海部分题解
    abc215_e Chain Contestant(状压dp)
    bzoj3238 差异(后缀数组+单调栈)
    NCD2019部分题解
  • 原文地址:https://www.cnblogs.com/zengwei/p/560260.html
Copyright © 2011-2022 走看看