CTSchmidt
CTSchmidt

Reputation: 1205

Remove every white space between tags using regex only

I try to detect spaces between HTML-Tags.

In my case:

[...]a lot of code[...]<strong>text with spaces. and dots</strong>[...]a lot of code[...]

my goal is to get:

[...]a lot of code[...]<strong>textwithspaces.anddots</strong>[...]a lot of code[...]

that's it. I tryed something like: (?<=<strong>.*)\s(?=</strong>) but it don't give me the spaces which I want to delete. It is important, that I only delete these spaces and no other which may be destroying the code

Upvotes: 1

Views: 360

Answers (2)

CTSchmidt
CTSchmidt

Reputation: 1205

As requested, here is my solution:

set(#scrape, $replace regular expression(#scrape, "</strong>\\s|\\s</strong>|</strong>|<strong>\\s|\\s<strong>|<strong>", ""), "Global")
set(#scrape, $replace regular expression(#scrape, "(?<=<strong>.*?)\\s(?=.*?</strong>)", ""), "Global")
set(#scrape, $replace regular expression(#scrape, "&quot;\\s|\\s&quot;|&quot;", ""), "Global")

Upvotes: 0

vks
vks

Reputation: 67978

import re
x="<strong>text with spaces</strong>"
x=pattern=re.sub(r"\s+","",x)

Upvotes: 1

Related Questions