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("<[^H1|^H2]*?>")
This seemed to work but it also allowed the HTML tag(basically anythin
with an H or a 1 or a 2) How can I get this to strip all tags except
H1 and H2. Any Help you could give would be great.
Steve 3 1732
Steveo <stephen_p_barr ett <at> hotmail.com> writes: I wanted to allow all H1 and H2 tags so i changed it to:
re.compile("<[^H1|^H2]*?>")
This seemed to work but it also allowed the HTML tag(basically anythin with an H or a 1 or a 2) How can I get this to strip all tags except H1 and H2. Any Help you could give would be great.
You probably want a lookahead assertion. From the docs at http://docs.python.org/lib/re-syntax.html:
(?!...)
Matches if ... doesn't match next. This is a negative lookahead assertion.
For example, Isaac (?!Asimov) will match 'Isaac ' only if it's not followed by
'Asimov'.
So I would write your example something like: re.sub(r'</?(?!H1|H2|/H1|/H2)[^>]*>', r'', '<a>sdfsa</a>')
'sdfsa' re.sub(r'</?(?!H1|H2|/H1|/H2)[^>]*>', r'', '<H1>sdfsa</a>')
'<H1>sdfsa' re.sub(r'</?(?!H1|H2|/H1|/H2)[^>]*>', r'', '<H1>sdfsa</H2>')
'<H1>sdfsa</H2>'
(I was too lazy to compile the re, but of course that's what you'd normally want
to do.)
Steve
Steveo wrote: 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) [...]
Instead of using REs, you might consider the StrippingParser
from the Python Cookbook: http://aspn.activestate.com/ASPN/Coo...n/Recipe/52281
It allows you to specify explicitly which tags you want to leave
intact, so you'll be able to change your mind later without futzing
about with a complex RE...
Miles
I wrote: re.sub(r'</?(?!H1|H2|/H1|/H2)[^>]*>', r'', '<a>sdfsa</a>') 'sdfsa'
Maybe slightly better: re.sub(r'<(?!/?(?:H1|H2))[^>]*>', r'', '<a>sdfsa</a>')
'sdfsa' re.sub(r'<(?!/?(?:H1|H2))[^>]*>', r'', '<H1>sdfsa</a>')
'<H1>sdfsa' re.sub(r'<(?!/?(?:H1|H2))[^>]*>', r'', '<H1>sdfsa</H2>')
'<H1>sdfsa</H2>' re.sub(r'<(?!/?(?:H1|H2))[^>]*>', r'', '<H2>sdfsa</H2>')
'<H2>sdfsa</H2>'
I've just grouped things a bit differently so that I only have to write H1 and
H2 once.
Steve This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Margaret MacDonald |
last post by:
I've been going mad trying to figure out how to do this--it should be
easy!
Allow the user to enter '\_sometext\_', i.e., literal backslash,
underscore, some text, literal backslash, underscore and, after
submitting via POST to a preg_replace filter, get back
'_sometext_' (i.e., the same thing with the literal backslashes
stripped)
|
by: Patrick |
last post by:
Hello, after learning that I was taking a class in VB.NET, I have been
drafted to solve all my companies VB/scripting problems - hey, I should know
everything; I've already taken 6 classes ;) I should have been quiet about
it, but then I would never be reimbursed. Oh well.
I have been asked to write a program to ping a NetBIOS name, get the IP, and
compare the 3rd octet to a list to get the computer's location. So far, I
can ping the IP,...
|
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;
|
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...
|
by: Lu |
last post by:
Hi, i am currently working on ASP.Net v1.0 and is encountering the following
problem. In javascript, I'm passing in:
"somepage.aspx?QSParameter=<RowID>Chèques</RowID>" as part of the query
string. However, in the code behind when I tried to get the query string
value by calling Request.QueryString("QSParameter"), the value I got is:
"<RowID>Chques</RowID>". The special character "è" has been stripped out.
The web.config file is...
| |
by: David Sawyer |
last post by:
I am trying to read in an HTML file and strip out the HTML
code so that all I have left is the text of the body.
Does anyone have any suggestions for doing this?
Any HTML stripping routines or objects that perform the
function?
|
by: Spondishy |
last post by:
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>
|
by: FFMG |
last post by:
Hi,
I have a form that allows users to comment, add entries and so on.
But what a lot of them do is copy and paste directly from MS Word to my
forms.
almost all browsers will accept the post and give the impression that
everything is saved properly.
But, that is not the case when it comes time to displaying the message
|
by: Michel Bouwmans |
last post by:
Hey everyone,
I'm trying to strip all script-blocks from a HTML-file using regex.
I tried the following in Python:
testfile = open('testfile')
testhtml = testfile.read()
regex = re.compile('<script\b*>(.*?)</script>', re.DOTALL)
result = regex.sub('', blaat)
|
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...
|
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...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |