【发布时间】:2017-06-28 14:51:51
【问题描述】:
我的字符串看起来像:
我的名字是 Jason
<and maybe>我只是看起来像<a kid>但我不是
我想用
标记所有看起来像<bla bla bla> 的东西
<span style='color:red'>bla bla bla></span>
这段代码:
string s = "My name is Jason <and maybe> I just seem like <a kid> but I'm not"; // the string with all the text inside
int start = s.IndexOf("<");
int end = s.IndexOf(">");
if (start >= 0 && end > 0)
{
string result = s.Substring(start + 1, end - start - 1);
elem.LayoutText = s.Replace("<" + result + ">" , "<span style='color:red'>" + "<" + result + ">" + "</span>");
}
只替换第一次出现,我怎样才能替换所有?
【问题讨论】:
-
这是学习正则表达式的好时机。
-
你需要将你的开始和结束的发现封装在一个数组中,直到它们都返回负数
-
你想要的结果是什么?
标签: c# .net regex visual-studio visual-studio-2015