使用正则表达式和replace替换一个字符串中截取的一字符串
aspx中:
<table>
<tr>
<td style="width: 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="width: 621px" colspan="2">
<asp:Button id="btnSubmit" runat="server" Text="截取" onclick="btnSubmit_Click" CssClass="btn_pic1"></asp:Button>
</td>
</tr>
<tr>
<td style="width: 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;
}
}