Spindel
Spindel

Reputation: 289

Remove whitespace before [quote-tag and after [/quote]

I need to remove whitespaces/newlines from a string but only before a [quote-tag and after a [/quote]. Other whitespaces/newlines should be untouched.

Example:

[quote=lorem]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ut aliquam elit, nec lobortis neque.[/quote]

Nunc ultrices, eros a fringilla varius, metus arcu rutrum libero, quis pulvinar sapien lorem et mauris. Nullam vitae nisi tristique, congue ipsum ut, lobortis augue.

Nam tempus justo hendrerit, feugiat ipsum eget, pellentesque eros. Fusce vel pretium felis, nec euismod nisl.








[quote=lipsum]Sed efficitur malesuada eleifend.[/quote]



Ut accumsan nulla sed lacus luctus, in venenatis diam tristique.

I want this to become:

[quote=lorem]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ut aliquam elit, nec lobortis neque.[/quote]
Nunc ultrices, eros a fringilla varius, metus arcu rutrum libero, quis pulvinar sapien lorem et mauris. Nullam vitae nisi tristique, congue ipsum ut, lobortis augue.

Nam tempus justo hendrerit, feugiat ipsum eget, pellentesque eros. Fusce vel pretium felis, nec euismod nisl.
[quote=lipsum]Sed efficitur malesuada eleifend.[/quote]
Ut accumsan nulla sed lacus luctus, in venenatis diam tristique.

Upvotes: 0

Views: 222

Answers (1)

user1467267
user1467267

Reputation:

I would simplify it by running the replacement in 2 rounds:

First round: \n+\[quote and replace it with [quote

Second round: quote\]\n+ and replace it with quote\]

I hope this helps you :)

You might strengthen the matching for tabs and spaces too, like so: [\n\t\s]+

Example:

newString = re.sub(r'[\n\t\s]+\[quote', '[quote', quoteString, re.S)
newString = re.sub(r'quote\][\n\t\s]+', 'quote]', newString, re.S)

..or even just wrap them around each other

Upvotes: 1

Related Questions