473,587 Members | 2,473 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 4149
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
1730
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
1875
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
6013
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...
258
8514
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
1250
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...
6
3071
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...
3
2412
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...
2
3818
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:...
1
16947
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
7843
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...
1
7967
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...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
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...
0
5392
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...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1185
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...

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.