我尝试搜索此内容,但只找到了PHP答案。我在Google App Engine上使用Python并尝试删除嵌套引号。
例如:
[quote user2]
[quote user1]Hello[/quote]
World
[/quote]
我想运行一些东西来获得最外层的引用。
[quote user2]World[/quote]
答案 0 :(得分:3)
不确定您是否只需要引号,或者删除了嵌套引号的整个输入。这个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
答案 1 :(得分:0)