473,725 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stripping html tags from text

Hi,

I'm looking for help with a regular expression and c#.

I want to remove all tags from a piece of html except the following.

<a>
<b>
<h1>
<h2>
<h3>

Also, <a> could be <a href="aa">aaa</a> etc.

Help would be appreciated, along with an explanation of the reg
expression created.

Thanks.

Mar 6 '06 #1
4 4169
HTML is complex. It would be better instead to say that you want to
*retrieve* *only* all of the following tags. That way, they are the only
tags the Regular Expression will have to look for.

The following will do this:

(?i)<\s*(a|br|h 1|h2|h3)[^>]*>(?:([^<\r\n]+)(?=(?:<\/\1)|(?:\r?\n))) ?

Note: Grouping is used in this Regular Expression. It groups the tag names
into Group 1, and the InnerText into Group 2, in case you need either of
these.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"Spondishy" <sp*******@tisc ali.co.uk> wrote in message
news:11******** *************@z 34g2000cwc.goog legroups.com...
Hi,

I'm looking for help with a regular expression and c#.

I want to remove all tags from a piece of html except the following.

<a>
<b>
<h1>
<h2>
<h3>

Also, <a> could be <a href="aa">aaa</a> etc.

Help would be appreciated, along with an explanation of the reg
expression created.

Thanks.

Mar 6 '06 #2


i use this in VB

Private Function stripHTML(ByVal strHTML) As String

Dim objRegExp As New System.Text.Reg ularExpressions .Regex("<(.|\n) +?>")

Return objRegExp.Repla ce(strHTML, "")

End Function

so the regex System.Text.Reg ularExpressions .Regex("<(.|\n) +?>")

does the trick

so in C# it would be ( i am a VB coder so don`t shoot me )

private string stripHTML(objec t strHTML)

{

System.Text.Reg ularExpressions .Regex objRegExp = new
System.Text.Reg ularExpressions .Regex("<(.|\n) +?>");

return objRegExp.Repla ce(strHTML, "");

}

regards

Michel Posseth [MCP]

"Spondishy" <sp*******@tisc ali.co.uk> wrote in message
news:11******** *************@z 34g2000cwc.goog legroups.com...
Hi,

I'm looking for help with a regular expression and c#.

I want to remove all tags from a piece of html except the following.

<a>
<b>
<h1>
<h2>
<h3>

Also, <a> could be <a href="aa">aaa</a> etc.

Help would be appreciated, along with an explanation of the reg
expression created.

Thanks.

Mar 6 '06 #3
The problem with that Regular Expression (in this case) is that it simply
matches all tags in the page. It doesn't match InnerText, as he requested,
and it matches end tags as separate matches. It is excellent for, for
example, stripping HTML tags from a page, but not for his requirements.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"m.posseth" <mi*****@nohaus ystems.nl> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..


i use this in VB

Private Function stripHTML(ByVal strHTML) As String

Dim objRegExp As New System.Text.Reg ularExpressions .Regex("<(.|\n) +?>")

Return objRegExp.Repla ce(strHTML, "")

End Function

so the regex System.Text.Reg ularExpressions .Regex("<(.|\n) +?>")

does the trick

so in C# it would be ( i am a VB coder so don`t shoot me )

private string stripHTML(objec t strHTML)

{

System.Text.Reg ularExpressions .Regex objRegExp = new
System.Text.Reg ularExpressions .Regex("<(.|\n) +?>");

return objRegExp.Repla ce(strHTML, "");

}

regards

Michel Posseth [MCP]

"Spondishy" <sp*******@tisc ali.co.uk> wrote in message
news:11******** *************@z 34g2000cwc.goog legroups.com...
Hi,

I'm looking for help with a regular expression and c#.

I want to remove all tags from a piece of html except the following.

<a>
<b>
<h1>
<h2>
<h3>

Also, <a> could be <a href="aa">aaa</a> etc.

Help would be appreciated, along with an explanation of the reg
expression created.

Thanks.


Mar 6 '06 #4
Oops :-)

i just read "Stripping html tags from text" and missed the exclusion part
except the following.

<a>
<b>
<h1>
<h2>
<h3>

Also, <a> could be <a href="aa">aaa</a> etc.

my code will convert
<html>
<head>
<body>
<table>
<tr><td>bla bla </td></tr>
</table>
</body>
</head>
</html>

into

bla bla
regards

Michel


"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:eI******** ******@TK2MSFTN GP09.phx.gbl... The problem with that Regular Expression (in this case) is that it simply
matches all tags in the page. It doesn't match InnerText, as he requested,
and it matches end tags as separate matches. It is excellent for, for
example, stripping HTML tags from a page, but not for his requirements.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"m.posseth" <mi*****@nohaus ystems.nl> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..


i use this in VB

Private Function stripHTML(ByVal strHTML) As String

Dim objRegExp As New System.Text.Reg ularExpressions .Regex("<(.|\n) +?>")

Return objRegExp.Repla ce(strHTML, "")

End Function

so the regex System.Text.Reg ularExpressions .Regex("<(.|\n) +?>")

does the trick

so in C# it would be ( i am a VB coder so don`t shoot me )

private string stripHTML(objec t strHTML)

{

System.Text.Reg ularExpressions .Regex objRegExp = new
System.Text.Reg ularExpressions .Regex("<(.|\n) +?>");

return objRegExp.Repla ce(strHTML, "");

}

regards

Michel Posseth [MCP]

"Spondishy" <sp*******@tisc ali.co.uk> wrote in message
news:11******** *************@z 34g2000cwc.goog legroups.com...
Hi,

I'm looking for help with a regular expression and c#.

I want to remove all tags from a piece of html except the following.

<a>
<b>
<h1>
<h2>
<h3>

Also, <a> could be <a href="aa">aaa</a> etc.

Help would be appreciated, along with an explanation of the reg
expression created.

Thanks.



Mar 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1736
by: Steveo | last post by:
I am currently stripping HTML from a string with the following code. (I know it's not the best way to strip HTML but bear with me) re.compile("<.*?>") I wanted to allow all H1 and H2 tags so i changed it to: re.compile("<*?>") This seemed to work but it also allowed the HTML tag(basically anythin
3
1883
by: shank | last post by:
I'm querying a text field with an 8000 character limit. The text also contains HTML tags like <p> <br> and more. Is there a way to strip all HTML tags in the resulting recordset, or do I have to replace each tag individually? thanks
15
6042
by: Jeff North | last post by:
Hi, I'm using a control called HTMLArea which allows a person to enter text and converts the format instructions to html tags. Most of my users know nothing about html so this is perfect for my use. http://www.interactivetools.com/products/htmlarea/ This only works with IE5.5+. What I need to do is to take this html formatted text and only display part of the text on a web page (much like a news article which shows only part of the...
258
8669
by: Terry Andersen | last post by:
If I have: struct one_{ unsigned int one_1; unsigned short one_2; unsigned short one_3; }; struct two_{ unsigned int two_1;
4
1261
by: Lance | last post by:
Hi, What way could I strip certain tags (like HTML comments) from the HTML being delivered to the client? I don't mean what regexp to use, but where do I put this stripping code? I'm thinking something in the Global.asax, but I can't find any reference to it having a Render or PreRender event or how to tie into them if I did! I'd ideally like some help along the lines of: 1 - Where to put the code 2 - example of how to alter the...
6
3079
by: Medros | last post by:
I understand that you can strip html out of a txt file so that all the information is left is the visable information that is needed (e.g. everything that has < > around is gone). My question is that I have a table of information that I need to be fed into a program as such. Well kind of I need the program to read it just as you would on paper and be able to use that information like it was entered. I am unsure how strip so much away just...
3
2424
by: Jason | last post by:
First things first, let me say that I couldn't decide whether to post this to the PHP ng, or to an XML ng. I know from experience that you guys know what you're talking about, though, and all of the questions mean "how to do this in PHP," so I hope I picked the right one ;-) For about a year, I've been importing Yahoo News headlines into my site via their RSS feed. But I would much rather import Google News headlines because I can make...
2
3824
by: Big Moxy | last post by:
I want to send html formatted text yet strip out special characters (e.g. quotes and semi colons). I've seen preg_replace examples like $messageout = preg_replace('/\(\)<>]/i','',$message); to preserve some additional characters but don't know how to approach preserving html in general. This is a typical message line: $message.= "<b>Date: </b>" . $today . "<br />"; I am setting these headers:
1
16973
by: since | last post by:
I figured I would post my solution to the following. Resizable column tables. Search and replace values in a table. (IE only) Scrollable tables. Sortable tables. It is based on a lot examples I found on the web. Works in IE and mozilla. http://www.imaputz.com/cssStuff/bigFourVersion.html
0
8874
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8748
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9393
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6000
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4775
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2622
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2151
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.