【发布时间】:2021-03-05 20:47:55
【问题描述】:
您好,我正在尝试解析剧本并尝试使用正则表达式捕获 (NAME) : (Dialogue)。到目前为止,在正则表达式 101 上,我有 re.complie('(\w+)\n(.*)'),但正如您在图像中看到的那样,对于某些包含特殊字符的行,它会变得平坦。任何帮助表示赞赏。 (添加文本格式以帮助重现性)
CLAIRE
Morning, beauty.
Caitlin lets out a grunt and rolls over onto her belly.
CLAIRE
Let's go. Or we'll never leave on time.
From out of the pillow comes Caitlin's voice.
CAITLIN
(muffled)
I'm totally ready.
Claire glances around at the piles of unpacked clothes.
CLAIRE
Come on, I'll make you some waffles,
maybe we'll squeeze in a trip to the
mall.
(beat)
Caitlin...
【问题讨论】:
-
您没有解释您期望的输出。
(?m)^\s*\b([A-Z]+)\b\s*\n(.*(?:\n.+)*)对你有用吗?见the regex demo。 -
谢谢,@WiktorStribiżew,它完美地捕捉到了第一组中的名称和第二组中的所有对话。我想要的输出是一个字典,其中键是第一组,值是第二组。我在 OP 中使用了该模式并编写了代码来执行此操作。现在有了你的模式,非常感谢。我会发布完整的答案,以防其他有需要的人偶然发现它。