I am dealing with dirty text data (and not with valid html). I am doing natural language processing and short code snippets shouldn't be removed because they can contain valuable information while long code snippets don't.
Thats why I would like to remove text between code tags only if the content that will be removed has character length > n
.
Let's say the number of allowed characters between two code tags is n <= 5
. Then everything between those tags that is longer than 5 characters will be removed.
My approach so far deletes all of the code characters:
text = "This is a string <code>1234</code> another string <code>123</code> another string <code>123456789</code> another string."
text = re.sub("<code>.*?</code>", '', text)
print(text)
Output: This is a string another string another string another string.
The desired output:
"This is a string <code>1234</code> another string <code>123</code> another string another string."
Is there a way to count the text length for all of the appearing <code ... </code>
tags before it will actually be removed?
"<code>[^>]{5,}</code>"