求一个替换的正则表达式

zhmvb 2008-02-20 03:11:44
把源字符串:“本次列车在<%请输入站名%>站停车2分。”
替换成为: “本次列车在<a herf=''>请输入站名</a>站停车2分。”
其中“<a herf=''>请输入站名</a>”中的字符串“请输入站名”是源字符串中的字符。

有一现成的正则,大家可以在此基础上改进
string sjnr = System.Text.RegularExpressions.Regex.Replace(textnr, "<%.+?%>", "~");
...全文
51 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
王集鹄 2008-02-20
  • 打赏
  • 举报
回复
string s = "由于前方故障,本次列车在<%请输入站名%>站停车<%请输入分钟数%>分钟。";
s = Regex.Replace(s, "<%(.*?)%>", "<a href=''>$1</a>");
Console.WriteLine(s);
//由于前方故障,本次列车在<a href=''>请输入站名</a>站停车<a href=''>请输入分钟数</a>分钟。
zhmvb 2008-02-20
  • 打赏
  • 举报
回复
public string Value
{
get
{
if (this.webBrowser.Document.Body != null)
return this.webBrowser.Document.Body.InnerText;
else
return "";
}
set
{
//由于前方故障,本次列车在<%请输入站名%>站停车<%请输入分钟数%>分钟。
if (this.webBrowser.Document.Body != null)
{
string sjnr = Regex.Replace(value, "<%.+?%>",new MatchEvaluator(CorrectString),RegexOptions.Compiled|RegexOptions.IgnoreCase);
this.webBrowser.Document.Body.InnerHtml = sjnr;
}
}
}
private static string CorrectString(Match match)
{
string matchValue = match.Value;
matchValue = "0000占";
return matchValue;
}

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧