搜索字符串并替换部分字符串

时间:2015-10-23 07:41:45

标签: c# .net

我有一个字符串,其中包含一些随机文本以及0个或更多span标记。

喜欢这个。

Hey <span class="OverLayStyle" style="background-color:#d8dfea">@Peter</span> and <span class="OverLayStyle" style="background-color:#d8dfea">@cara</span> 

Do you like <span class="OverLayStyle" style="background-color:#d8dfea">#Facebook</span>  or <span class="OverLayStyle" style="background-color:#d8dfea">#Google</span> ?

我想提取包含@的span tag并将其替换为a tag,如下所示:

<span class="OverLayStyle" style="background-color:#d8dfea">@Peter</span>

<a href="" class="OverLayStyle" style="background-color:#d8dfea">@Peter</a>

是否可以做这样的事情?

3 个答案:

答案 0 :(得分:3)

您可以执行以下操作:

var line = @"
<span class=""OverLayStyle"" style=""background-color:#d8dfea"">@Peter</span>
<span class=""OverLayStyle"" style=""background-color:#d8dfea"">Peter</span>
<span class=""OverLayStyle"" style=""background-color:#d8dfea"">someone@example.com</span>
<span class=""OverLayStyle"" style=""background-color:#d8dfea"">@Peter</span>";

var replaced = Regex.Replace(
    line,
    "<span.*?>@+.*?</span>",
    match => match.Value
             .Replace("span ", "a href=\"\" ")
             .Replace("/span>", "/a>"));


Gives:
  <a href="" class="OverLayStyle" style="background-color:#d8dfea">@Peter</a>
  <span class="OverLayStyle" style="background-color:#d8dfea">Peter</span>
  <span class="OverLayStyle" style="background-color:#d8dfea">someone@example.com</span>
  <a href="" class="OverLayStyle" style="background-color:#d8dfea">@Peter</a>

当内容以@

开头时,这只会替换span标记

答案 1 :(得分:2)

你可以试试这个:

    string s1="Hey <span class=\"OverLayStyle\" style=\"background-color:#d8dfea\">@Peter</span> and <span class=\"OverLayStyle\" style=\"background-color:#d8dfea\">@cara</span>";
    string s2 = "Do you like <span class=\"OverLayStyle\" style=\"background-color:#d8dfea\">#Facebook</span>  or <span class=\"OverLayStyle\" style=\"background-color:#d8dfea\">#Google</span> ?";

    var sb=new StringBuilder();

    var parts = s1.Split(new string[] { "</span>" },StringSplitOptions.RemoveEmptyEntries);
    foreach (var s in parts) {
        if (s.Contains('@'))
            sb.Append(s.Replace("<span ", "<a href=\"\" ") + "</a>");
        else
            sb.Append(s + "</span>"); 
    }

    var resultOfs1 = sb.ToString();
    /*
    Hey <a href="" class="OverLayStyle" style="background-color:#d8dfea">@Peter</a> and <a href="" class="OverLayStyle" style="background-color:#d8dfea">@cara</a> 
    */

    sb.Clear();

    parts = s2.Split(new string[] { "</span>" }, StringSplitOptions.RemoveEmptyEntries);
    foreach (var s in parts) {
        if (s.Contains('@'))
            sb.Append(s.Replace("<span ", "<a href=\"\" ") + "</a>");
        else
            sb.Append(s + "</span>");
    }
    var resultOfs2 = sb.ToString();
    /*
    Do you like <span class="OverLayStyle" style="background-color:#d8dfea">#Facebook</span>  or <span class="OverLayStyle" style="background-color:#d8dfea">#Google</span> ?</span> 
    */

答案 2 :(得分:-1)

你试过string.Replace吗? :

string v=yourString.Replace(@"<span",@"yourReplacement"); string v2=yourString.Replace(@"</span",@"yourReplacement");

要避免任何错误,您必须分配给新字符串

来源:http://www.dotnetperls.com/replace