473,230 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

re.sub does not replace all occurences

Hello everybody,

I wanted to use re.sub to strip all HTML tags out of a given string. I
learned that there are better ways to do this without the re module,
but I would like to know why my code is not working. I use the
following:

def stripHtml(source):
source = re.sub("[\n\r\f]", " ", source)
source = re.sub("<.*?>", "", source, re.S | re.I | re.M)
source = re.sub("&(#[0-9]{1,3}|[a-z]{3,6});", "", source, re.I)
return source

But the result still has some tags in it. When I call the second line
multiple times, all tags disappear, but since HTML tags cannot be
overlapping, I do not understand this behavior. There is even a
difference when I omit the re.I (IGNORECASE) option. Without this
option, some tags containing only capital letters (like </FONT>) were
kept in the string when doing one processing run but removed when
doing multiple runs.

Perhaps anyone can tell me why this regex is behaving like this.

Thanks and regards,
Christoph

Aug 7 '07 #1
3 11109
On Tue, 07 Aug 2007 10:28:24 -0700, Christoph Krammer wrote:
Hello everybody,

I wanted to use re.sub to strip all HTML tags out of a given string. I
learned that there are better ways to do this without the re module,
but I would like to know why my code is not working. I use the
following:

def stripHtml(source):
source = re.sub("[\n\r\f]", " ", source)
source = re.sub("<.*?>", "", source, re.S | re.I | re.M)
source = re.sub("&(#[0-9]{1,3}|[a-z]{3,6});", "", source, re.I)
return source

But the result still has some tags in it. When I call the second line
multiple times, all tags disappear, but since HTML tags cannot be
overlapping, I do not understand this behavior. There is even a
difference when I omit the re.I (IGNORECASE) option. Without this
option, some tags containing only capital letters (like </FONT>) were
kept in the string when doing one processing run but removed when
doing multiple runs.
Can you give some example HTML where it fails?

Ciao,
Marc 'BlackJack' Rintsch
Aug 7 '07 #2
On 2007-08-07, Christoph Krammer <re********@googlemail.comwrote:
Hello everybody,

I wanted to use re.sub to strip all HTML tags out of a given string. I
learned that there are better ways to do this without the re module,
but I would like to know why my code is not working. I use the
following:

def stripHtml(source):
source = re.sub("[\n\r\f]", " ", source)
source = re.sub("<.*?>", "", source, re.S | re.I | re.M)
source = re.sub("&(#[0-9]{1,3}|[a-z]{3,6});", "", source, re.I)
return source

But the result still has some tags in it. When I call the
second line multiple times, all tags disappear, but since HTML
tags cannot be overlapping, I do not understand this behavior.
There is even a difference when I omit the re.I (IGNORECASE)
option. Without this option, some tags containing only capital
letters (like </FONT>) were kept in the string when doing one
processing run but removed when doing multiple runs.

Perhaps anyone can tell me why this regex is behaving like
this.
>>import re
help(re.sub)
Help on function sub in module re:

sub(pattern, repl, string, count=0)
Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
replacement repl. repl can be either a string or a callable;
if a callable, it's passed the match object and must return
a replacement string to be used.

And from the Python Library Reference for re.sub:

The pattern may be a string or an RE object; if you need to
specify regular expression flags, you must use a RE object,
or use embedded modifiers in a pattern; for example,
"sub("(?i)b+", "x", "bbbb BBBB")" returns 'x x'.

The optional argument count is the maximum number of pattern
occurrences to be replaced; count must be a non-negative
integer. If omitted or zero, all occurrences will be
replaced. Empty matches for the pattern are replaced only
when not adjacent to a previous match, so "sub('x*', '-',
'abc')" returns '-a-b-c-'.

In other words, the fourth argument to sub is count, not a set of
re flags.

--
Neil Cerutti
Aug 7 '07 #3
Neil Cerutti schrieb:
In other words, the fourth argument to sub is count, not a set of
re flags.
I knew it had to be something very stupid.

Thanks a lot.

Aug 7 '07 #4

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

Similar topics

4
by: Prasad S | last post by:
Hello I wish to replace all the characters in a string except those which are inside '<' & '>' characters. And there could be multiple occurences of < & > within the string. e.g. string =...
3
by: Jim | last post by:
Hey all, I have a dilemma. I have a field in a table called "Supervisor." The table currently has 1,500+ records. Sometimes, the HR download that populates the field formats the field one way,...
4
by: Dominique Deleris | last post by:
Hello, I desperately and urgently need a __working__ function, that will replace all occurences of a substring in a string. Prototype should be : char *str_replace(char *str, const char...
4
by: serge | last post by:
I managed to put together C# code and have it do the following: 1- Get all the table names that start with the letter "Z" from sysobjects of my SQL 2000 database and put these table names...
12
by: Michael | last post by:
In PHP there is a function called str_replace (http://php.net/str_replace). Basically you can freed in two strings and a "subject" string. Then it goes through the subject string searching for...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

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.