【问题标题】:Remove nested bbcode quotes in Python?删除 Python 中嵌套的 bbcode 引号?
【发布时间】:2011-12-10 18:56:56
【问题描述】:

我尝试搜索此内容,但只找到 PHP 答案。我在 Google App Engine 上使用 Python 并尝试删除嵌套引号。

例如:

[quote user2]
[quote user1]Hello[/quote]
World
[/quote]

我想运行一些东西来获得最外面的报价。

[quote user2]World[/quote]

【问题讨论】:

标签: python bbcode


【解决方案1】:

不确定您是只想要引号,还是删除嵌套引号的整个输入。此 pyparsing 示例同时执行以下操作:

stuff = """
Other stuff
[quote user2] 
[quote user1]Hello[/quote] 
World 
[/quote] 
Other stuff after the stuff
"""

from pyparsing import (Word, printables, originalTextFor, Literal, OneOrMore, 
    ZeroOrMore, Forward, Suppress)

# prototype username
username = Word(printables, excludeChars=']')

# BBCODE quote tags
openQuote = originalTextFor(Literal("[") + "quote" + username + "]")
closeQuote = Literal("[/quote]")

# use negative lookahead to not include BBCODE quote tags in tbe body of the quote
contentWord = ~(openQuote | closeQuote) + (Word(printables,excludeChars='[') | '[')
content = originalTextFor(OneOrMore(contentWord))

# define recursive definition of quote, suppressing any nested quotes
quotes = Forward()
quotes << ( openQuote + ZeroOrMore( Suppress(quotes) | content ) + closeQuote )

# put separate tokens back together
quotes.setParseAction(lambda t : '\n'.join(t))

# quote extractor
for q in quotes.searchString(stuff):
    print q[0]

# nested quote stripper
print quotes.transformString(stuff)

打印:

[quote user2]
World
[/quote]

Other stuff
[quote user2]
World
[/quote] 
Other stuff after the stuff

【讨论】:

    【解决方案2】:

    您应该在 Python 中找到并使用真正的 BBCode 解析器。谷歌搜索带来了一些点击 - 例如this onethis one

    【讨论】:

    • 哦,我其实用的是第一个!但是当我测试它时,报价一直在继续。没想到也许能解决这个问题,我去看看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多