您的位置:首页 > 其它

使用正则表达式和replace替换一个字符串中截取的一字符串

2006-11-14 13:03 936 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: