使用正则表达式和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;
}
}
<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;
}
}
相关文章推荐
- JS使用replace()方法和正则表达式进行字符串的搜索与替换实例
- JS使用replace()方法和正则表达式进行字符串的搜索与替换实例
- JS使用replace()方法和正则表达式进行字符串的搜索与替换实例
- 使用正则表达式替换字符串中的字符
- ActionScript 3.0 学习(九) AS3 一个应用正则表达式替换字符串的例子
- Notepad++ 使用正则表达式查找替换字符串
- PHP 字符串正则替换函数preg_replace使用说明
- str_replace() 函数使用一个字符串替换字符串中的另一些字符
- vi 中使用正则表达式搜索,替换字符串
- 使用正则表达式,字符串的批量替换
- [Javascript] String.replace 正则表达式及替换函数使用 $ 字符
- 一个非常好用的正则表达式替换字符串包含的域名
- MySQL中使用replace、regexp进行正则表达式替换的用法分析
- 关于C#正则表达式十万火急 截取一个字符串中的子串 高手进了呀!!!!!
- Java基础之一组有用的类——使用正则表达式查找和替换(SearchAndReplace)
- JS使用正则表达式截取字符串中的中文
- Java使用正则表达式截取重复出现的XML字符串功能示例
- PHP 字符串正则替换函数preg_replace使用说明
- 还为WORD替换特殊字符串烦恼吗?请使用word通配符其实和正则表达式很类似,学会使用起到事半功倍的效果!