1

Regex ReplaceC#.netメソッド を使用して、ここからすべての追加のFacebook情報を置き換えるのを手伝ってください。

<a href="/l.php?u=http%3A%2F%2Fon.fb.me%2FOE6gnB&amp;h=yAQFjL0pt&amp;s=1" target="_blank" rel="nofollow nofollow" onmouseover="LinkshimAsyncLink.swap(this, &quot;http:\/\/on.fb.me\/OE6gnB&quot;);" onclick="LinkshimAsyncLink.swap(this, &quot;\/l.php?u=http\u00253A\u00252F\u00252Fon.fb.me\u00252FOE6gnB&amp;h=yAQFjL0pt&amp;s=1&quot;);">http://on.fb.me/OE6gnB</a>somehtml

出力

somehtml <a href="http://on.fb.me/OE6gnB">on.fb.me/OE6gnB</a> somehtml

正規表現をフォローしようとしましたが、うまくいきませんでした

searchPattern = "<a([.]*)?/l.php([.]*)?(\">)?([.]*)?(</a>)?";
replacePattern = "<a href=\"$3\" target=\"_blank\">$3</a>";

ありがとう

4

2 に答える 2

2

私は次のコードで正規表現を使用してこれを行うことができます

 searchPattern = "<a(.*?)href=\"/l.php...(.*?)&amp;?(.*?)>(.*?)</a>";
          string html1 = Regex.Replace(html, searchPattern, delegate(Match oMatch)
    {
        return string.Format("<a href=\"{0}\" target=\"_blank\">{1}</a>", HttpUtility.UrlDecode(oMatch.Groups[2].Value), oMatch.Groups[4].Value);

    });
于 2012-09-05T10:25:48.420 に答える
1

これを試すことができます(使用するにはSystem.Webを追加する必要がありますSystem.Web.HttpUtility):

        string input = @"<a href=""/l.php?u=http%3A%2F%2Fon.fb.me%2FOE6gnB&amp;h=yAQFjL0pt&amp;s=1"" target=""_blank"" rel=""nofollow nofollow"" onmouseover=""LinkshimAsyncLink.swap(this, &quot;http:\/\/on.fb.me\/OE6gnB&quot;);"" onclick=""LinkshimAsyncLink.swap(this, &quot;\/l.php?u=http\u00253A\u00252F\u00252Fon.fb.me\u00252FOE6gnB&amp;h=yAQFjL0pt&amp;s=1&quot;);"">http://on.fb.me/OE6gnB</a>somehtml";
        string rootedInput = String.Format("<root>{0}</root>", input);
        XDocument doc = XDocument.Parse(rootedInput, LoadOptions.PreserveWhitespace);

        string href;
        var anchors = doc.Descendants("a").ToArray();
        for (int i = anchors.Count() - 1; i >= 0;  i--)
        {
            href = HttpUtility.ParseQueryString(anchors[i].Attribute("href").Value)[0];

            XElement newAnchor = new XElement("a");
            newAnchor.SetAttributeValue("href", href);
            newAnchor.SetValue(href.Replace(@"http://", String.Empty));

            anchors[i].ReplaceWith(newAnchor);
        }
        string output = doc.Root.ToString(SaveOptions.DisableFormatting)
                        .Replace("<root>", String.Empty)
                        .Replace("</root>", String.Empty);
于 2012-08-14T08:31:31.377 に答える