Skip to main content
 首页 » 编程设计

python之使用正则表达式从电影脚本中提取对话

2025年02月15日41Terrylee

我想像这样提取电影剧本对话:

  • 大写字符名称
  • 对话跟进直到换行,以避免旁白也被抢夺。

Current Regex: ((\s[^\w].\s[A-Z]+)\n+.+)

问题是,它只从对话中提取角色名称和第一句话。

测试数据如下:

                                  ADAM 
                         Help! Someone help me! (He stops when  
                         he hears a loud dragging sound somewhere  
                         in the room. He looks out into the darkness  
                         and calls out.) Is someone there? Hey!  
                         (He turns back to the corner to which  
                         he is chained, says in a slightly softer  
                         but still panicked voice) Shit, Im  
                         probably dead. 
 
               Suddenly, from out within the darkness comes a mans low, raspy  
               voice. It startles Adam. The voice, we will soon learn, belongs  
               to LAWRENCE. 
 
                                     LAWRENCE 
                         Youre not dead. 
 
               Adam quickly turns in the direction of the voice. Holding his  
               arms out for balance, he tries to look across the room to whoever  
               is speaking, but still cannot see a thing. 
 
                                     ADAM 
                         Whos that? Whos that?! 
 
                                     LAWRENCE 
                         (his voice strangely a bit on the calm  
                         side, and almost irritated with Adams  
                         reaction to the situation. This shall  
                         be his tone for many scenes to come.)  
                         Theres no point in yelling, I already  
                         tried it. 
 
                                     ADAM 
                         Turn on the lights! 
 
                                     LAWRENCE 
                         Would if I could. 
 
                                     ADAM 
                         What the fuck is going on? Where am  
                         I? (He turns into his corner, touching  
                         the wall.) 
 
                                     LAWRENCE 
                         I dont know yet. 
 
                                     ADAM 
                         (smelling something; in disgust) What  
                         is that smell? 
 
                                     LAWRENCE 
                         Shh! Hang on a second, I think I found  
                         something. 
 
               With a loud click and an even louder buzzing sound, the very  
               bright fluorescent lights come to life, lighting up in rows,  
               starting from Lawrences end and moving towards Adam. As they  
               come on, Adam is nearly blinded by the sudden change from pitch  
               black to bright white and squints in pain, holding up his arms  
               to cover his face. In the light we now see that he is in his  
               mid-twenties, with short brown hair, wearing a dark blue striped  
               shirt over a white tee shirt and jeans, looking like a drowned  
               rat from the tub. It takes him a moment but his eyes finally  
               start to adjust, and he looks around the room. He and we see  
               Lawrence, who also winces from the glare of the lights, standing  
               by the light switch and the door. He is on the opposite end of  
               the room, also chained to a pipe in the corner by his foot. He  
               wears a blue button-down dress shirt, now soaked with sweat stains.  
               He is middle aged, mid to late forties, with pale blonde hair  
               and even paler skin. Dark circles are under his eyes. Both men  
               are barefoot. 
 
               Lawrences eyes adjust to the light and he sees across the room.  
               Then, his gaze starts towards the center of the room, as does  
               Adams, who steps forward as much as he can, a look of horror  
               on his face. We see lying face down the body of a man who has  
               blown his brains out, lying in a pool of blood, clad in only  
               boxer shorts and a tee shirt. In his left hand is a gun, in his  
               right hand is a micro cassette recorder. A gunshot and a scream  
               are heard as the camera moves up and in a fast 360° angle above  
               and circling the man, ending in a full overhead view of him. 
 
 
               The shot cuts to Adam, who reels in shock and disgust. 
 
                                     ADAM 
                         Holy shit! 
 
               He turns towards the tub and leans over, gagging and coughing.  
               Lawrence in the meantime hops forward the best he can, studying  
               the body with a look of fear and concern. Adam stops coughing  
               and turns back around, takes another look at the body and around  
               the room. He looks down at his chain then starts to completely  
               freak out, grabbing and pulling at his chain. 
 
                                     ADAM 
                         (screaming) HELP!!! (He falls back onto  
                         his bottom on the floor as he yanks  
                         at the chain as hard as he can.) HELP!!!  
                         Help! 
 
               Lawrence just stands and watches him with an almost embarrassed,  
               appalled look at his behaviour. It seems that Lawrence, despite  
               being in the situation hes in, is above that kind of uncontrolled  
               reaction. He speaks a bit coldly. 
 
                                     LAWRENCE SANDLER 
                         No one can hear you. 

编辑

New Regex: (\w[A-Z]+\n\s).+?(?=\n)

请您参考如下方法:

您可以使用以下正则表达式:

(?:([A-Z]+ *[A-Z]+)\n).*?(?=$|([A-Z]+ *[A-Z]+)\n) 

解释:

(?:([A-Z]+ *[A-Z]+)\n)

  • 非捕获组仅匹配大写单词,中间允许有 0 个或更多空格,后跟行尾

.*?

  • 尽可能少地匹配

(?=$|([A-Z]+ *[A-Z]+)\n)

  • 积极前瞻断言新对话的结束或开始,如上文所述。

Demo