Удалить вложенные кавычки bbcode в Python? - PullRequest
1 голос
/ 10 декабря 2011

Я попытался найти это и нашел только ответы на PHP.Я использую Python в Google App Engine и пытаюсь удалить вложенные кавычки.

Например:

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

Я хотел бы запустить что-то, чтобы получить только внешнюю цитату.1006 *

[quote user2]World[/quote]

Ответы [ 2 ]

3 голосов
/ 10 декабря 2011

Не уверен, хотите ли вы просто кавычки или весь ввод с удаленными вложенными кавычками.Этот образец разбора выполняет оба действия:

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 голосов
/ 10 декабря 2011

Вы должны найти и использовать настоящий анализатор BBCode в Python. Googling вызывает несколько хитов - например, этот , и этот .

...