在[quote-tag之后和[/ quote]之后删除空格

时间:2014-09-08 20:42:44

标签: python regex string newline

我需要从字符串中删除空格/换行符,但只能在[quote-tag之后和[/ quote]之后删除。其他空格/换行应该不受影响。

示例:

[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.

我希望这成为:

[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.

1 个答案:

答案 0 :(得分:1)

我会通过在2轮中运行替换来简化它:

第一轮:\n+\[quote并将其替换为[quote

第二轮:quote\]\n+并将其替换为quote\]

我希望这可以帮助你:)

您也可以加强标签和空格的匹配,如下所示:[\n\t\s]+

示例:

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

..甚至只是将它们包裹在一起