删除Python中嵌套的bbcode引号?

发布于 2024-12-20 12:20:11 字数 245 浏览 0 评论 0原文

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

例如:

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

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

[quote user2]World[/quote]

I tried searching for this and only found PHP answers. I'm using Python on Google App Engine and am trying to remove nested quotes.

For example:

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

I would like to run something to just get the outer most quote.

[quote user2]World[/quote]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情徒 2024-12-27 12:20:11

不确定您是否只需要引号,或者删除嵌套引号的整个输入。这个 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

Not sure if you just want the quotes, or the whole input with nested quotes removed. This pyparsing sample does both:

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)

Prints:

[quote user2]
World
[/quote]

Other stuff
[quote user2]
World
[/quote] 
Other stuff after the stuff
飘过的浮云 2024-12-27 12:20:11

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

You should find and use a real BBCode parser in Python. Googling brings up some hits - for example this one, and this one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文