删除Python中的嵌套bbcode引号?

删除Python中的嵌套bbcode引号?,python,bbcode,Python,Bbcode,我试图搜索这个,但只找到了PHP的答案。我正在谷歌应用程序引擎上使用Python,并试图删除嵌套的引号 例如: [quote user2] [quote user1]Hello[/quote] World [/quote] 我想运行一些东西来获取最外层的报价 [quote user2]World[/quote] 您应该在Python中找到并使用真正的BBCode解析器。谷歌搜索会带来一些点击-例如,和。不确定您是否只想删除引号,或者删除带有嵌套引号的整个输入。此pyparsing示例执行以下

我试图搜索这个,但只找到了PHP的答案。我正在谷歌应用程序引擎上使用Python,并试图删除嵌套的引号

例如:

[quote user2]
[quote user1]Hello[/quote]
World
[/quote]
我想运行一些东西来获取最外层的报价

[quote user2]World[/quote]

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

不确定您是否只想删除引号,或者删除带有嵌套引号的整个输入。此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)

-1:没有表现出研究的努力。你试过什么?堆栈溢出不是来帮你完成所有工作的。@ChrisMorgan:我觉得你太苛刻了,请阅读:哦,我实际上在使用第一个!但是当我测试它的时候,引用一直在继续。我不认为它能够解决这个问题,我会检查一下。
[quote user2]
World
[/quote]

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