2011-12-10 18 views
1

我試圖尋找這個,只發現PHP的答案。我在Google App Engine上使用Python,並試圖刪除嵌套引號。在Python中刪除嵌套的bbcode引號?

例如:

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

我想運行的東西只得到最外面的報價。

[quote user2]World[/quote] 
+0

-1:不顯示的研究工作。你有什麼嘗試?堆棧溢出不在這裏爲你做所有的工作。 –

+0

@ChrisMorgan:我覺得你太苛刻了 –

+0

@EliBendersky:也許吧。 –

回答

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 
0

您應該在Python中找到並使用真正的BBCode解析器。谷歌搜索帶來了一些點擊 - 例如this onethis one

+0

哦,我實際上使用第一個!但是當我測試它時,引號一直在進行。我不認爲它可以解決這個問題,我會檢查一下。 – TylerW