【发布时间】:2015-06-30 10:39:18
【问题描述】:
我正在制作一个应用程序,它将角色扮演风格的消息翻译成更通用的东西。用户可以指定他们的偏好,例如:
Moves
- /me <move>
- *<move>*
Speech
- <speech>
- "<speech>"
Out-of-Character
- [<ooc>]
- ((ooc))
- //ooc
我需要解析这样的消息:
/me eats food "This is *munch* good!" [You're good at this]
或者像这样:
*eats food* This is *munch* good! ((You're good at this))
变成一个更通用的、类似 XML 的字符串:
<move>eats food <speech>This is <move>munch</move> good!</speech> <ooc>You're good at this</ooc></move>
但是关于哪个在哪个里面。例如:
*eats food "This is munch* good" // You're good at this
应该被解析为:
<move>eats food "This is munch</move><speech> good" </speech><ooc> You're good at this</ooc>
即使那不是用户想要的。请注意,最后一个示例中的引号没有被解析,因为它们没有包装完整的段,并且当前移动段在遇到第一个时还没有完成,并且在第二个时语音已经开始,并且第二个在它之后没有另一个围绕一个单独的语音片段。
我尝试过迭代地、递归地、使用树甚至使用正则表达式来执行此操作,但我还没有找到像我想要的那样工作的解决方案。 如何将上述 RP 样式消息解析为上述通用 XML 样式消息?
同样重要的是保留间距。
以下是使用上述首选项的其他示例:
I like roller coasters.
[what are you like?]
/me eats a hamburger // wanna grab lunch after this?
*jumps up and down* This ((the party)) is great!
/me performs *an action* within an action "And that's just fine [As is *an action* in ooc in speech]"
And messages /me can change contexts // at any point
[But ill-formatted ones *must be parsed] according "to* the rules"
-And text formatted in <non-specified ways> is ¬ treated; specially-
变成:
<speech>I like roller coasters.</speech>
<ooc>what are you like?</ooc>
<move>eats a hamburger <ooc> wanna grab lunch after this?</ooc></move>
<move>jumps up and down</move><speech> This <ooc>the party</ooc> is great!</speech>
<move>performs <move>an action</move> within an action <speech>And that's just fine <ooc>As is <move>an action</move> in ooc in speech</ooc></speech></move>
<speech>And messages <move>can change contexts <ooc> at any point</ooc></move></speech>
<ooc>But ill-formatted ones *must be parsed</ooc><speech> according <speech>to* the rules</speech></speech>
<speech>-And text formatted in <non-specified ways> is &not treated; specially-</speech>
【问题讨论】:
标签: algorithm text-parsing string-parsing