2011-07-26 6 views
0

私は内部にHTMLを含むテキスト領域を含むWebページを持っています。例えばここにテキストのサンプルです:HTML識別子を含む文字列からテキストを解析するにはどうすればよいですか?

var a = "some text follows<p><p>Give the following test text:</p> 
<pre>abc {<br /> int size;<br /> String name;<br /> Test (String name, int size) {<br />  this.name = name;<br />  this.size = size;<br /> }<br>" 

私は、HTMLが削除された後に上記のテキストで私のWebページのMETAの説明を設定します。

テキストからHTMLをどのように削除することができますか?私は何も想像する必要はありません。たぶん、角括弧を含むすべてのものを取り除いても、そのトリックを行うだろうか?

+0

可能重複http://stackoverflow.com/questions/1732348/regex-match- open-tags-except-xhtml-self-contained-tags) –

+0

の可能な複製[C#でhtmlを解析する最善の方法は何ですか?](http://stackoverflow.com/questions/56107/what-is-the-最善の方法-html-in-c) – outis

答えて

0

これが役立つことがあります。

var a = "some text follows<p><p>Give the following test text:</p>" 

var newString = Regex.Replace(a, @"<(.|\n)*?>", string.Empty); 

結果

+0

downvoterにdownvoteの理由をここで指定できますか?これは意味を理解している。これは間違った解決策ではないので、ポスターは「私は何も想像以上にする必要はありません。たぶん、角括弧を含むすべてのものを取り除くだけで、そのトリックを行うことになるでしょう... –

+0

投票者の心配はありません。彼らは散歩する。幸せなコーディング!!!!! – Asdfg

0

使用

string html = "your html text"; 
string result = System.Web.HttpUtility.HtmlEncode(html); 

または:

string html = "your html text"; 
string result = System.Security.SecurityElement.Escape(html); 

テスト:

var a = "some text follows<p><p>Give the following test text:</p> 
<pre>abc {<br /> int size;<br /> String name;<br /> Test (String name, int size) {<br />  this.name = name;<br />  this.size = size;<br /> }<br>" 

結果:

"some text follows&lt;p&gt;&lt;p&gt;Give the following test text:&lt;/p&gt; 
&lt;pre&gt;abc {&lt;br /&gt; int size;&lt;br /&gt; String name;&lt;br /&gt; Test (String name, int size) {&lt;br /&gt;  this.name = name;&lt;br /&gt;  this.size = size;&lt;br /&gt; }&lt;br&gt;" 
[XHTMLの自己完結型のタグを除いて正規表現一致オープンタグ(の
関連する問題