2012-02-21 7 views
1

私は動作する非常に良い正規表現を持っていて、クリック可能な文字列のURLを置き換えることができます。Regex URL画像と既存のリンクを置き換える、無視する

string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&amp;~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])"; 

ここで、クリック可能なリンクと画像を無視する方法を教えてください。

だから、文字列の下に無視:

<a href="http://www.someaddress.com">Some Text</a> 

<img src="http://www.someaddress.com/someimage.jpg" /> 

例:

The website www.google.com, once again <a href="http://www.google.com">www.google.com</a>, the logo <img src="http://www.google.com/images/logo.gif" /> 

結果:

The website <a href="http://www.google.com">www.google.com</a>, once again <a href="http://www.google.com">www.google.com</a>, the logo <img src="http://www.google.com/images/logo.gif" /> 

完全なHTMLパーサコード:

string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&amp;~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])"; 
Regex r = new Regex(regex, RegexOptions.IgnoreCase); 

text = r.Replace(text, "<a href=\"$1\" title=\"Click to open in a new window or tab\" target=\"&#95;blank\" rel=\"nofollow\">$1</a>").Replace("href=\"www", "href=\"http://www"); 

return text; 
+1

HtmlParserで簡単に、ハード維持するか、読みにくい、良いに... –

+0

あなたは正規表現でHTMLを解析しようとしていますか? –

+0

私はこれに既に答えました[ここ](http://stackoverflow.com/a/8833696/626273) – stema

答えて

2

最初に私は誰にも他人がいなければ義務的なリンクを投稿します。 RegEx match open tags except XHTML self-contained tags


どのようにこのような"のために/の後ろに否定先読みの使用に関する:

string regex = @"(?<!"")((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&amp;~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])(?!"")";

+1

投稿後の対処方法の提供をやめてください義務的な参照に... – jessehouwing

+0

合意しましたが、私は答えとして役に立たないコメントを投稿したくありません。 –

+0

これは私のために働いた: '(?<!\ w?=" ")((http | https | ftp | news | file)+://)[_.a-z0-9-] + \ ## |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~! )])) ' – Cindro

1

チェックアウト:Detect email in text using regexを、単にリンクについては正規表現を置き換える、それは内部のリンクを置き換えることはありませんタグ、コンテンツのみ。

http://htmlagilitypack.codeplex.com/

ような何か:


string textToBeLinkified = "... your text here ..."; 
const string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&amp;~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])"; 
Regex urlExpression = new Regex(regex, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); 

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
doc.LoadHtml(textToBeLinkified); 

var nodes = doc.DocumentNode.SelectNodes("//text()[not(ancestor::a)]") ?? new HtmlNodeCollection(); 
foreach (var node in nodes) 
{ 
    node.InnerHtml = urlExpression.Replace(node.InnerHtml, @"<a href=""$0"">$0</a>"); 
} 
string linkifiedText = doc.DocumentNode.OuterHtml; 
関連する問題