Skip to main content
 首页 » 编程设计

python之使用 python 正则表达式提取句子

2025年02月15日31del

我有一个如下的 Markdown 文件:

#2016-12-24 
| 单词 | 解释 | 例句 | 
| --------- | -------- | --------- | 
|**accelerator;**| - | - | 
|**compass**| - | - | 
|**wheels**| - | - | 
|**fabulous**| - | - | 
|**sweeping**| - | - | 
|**prospect**| - | - | 
|**pumpkin**| - | - | 
|**trolley**| - | - | 
|snapped,**| - | - | 
|tip| - | - | 
|lap| - | - | 
|tether.| - | - | 
|damp| - | - | 
|triumphant| - | - | 
|sarcastic| - | - | 
|missed out| - | - | 
|sidekick| - | - | 
|considerable| - | - | 
|Willow.| - | - | 
|eagle.| - | - | 
|considerably.| - | - | 
|flat.| - | - | 
|feast| - | - | 
|scramble| - | - | 
|turned up| - | - | 
|rounded off| - | - | 
|rat| - | - | 
|resembled| - | - | 
|By the time she had clambered back into the car,| - | - | 
|By the time she had clambered back into the car, they were running very late,| - | - | 
|wheeled his trolley| - | - | 
|barrier,| - | - | 
|bounced| - | - | 
|in blazes| - | - | 
|clutching| - | - | 
|sealed| - | - | 
|stunned.| - | - | 
|‘We’re stuck,| - | - | 
|marched off| - | - | 
|accelerator| - | - | 
|and the prospect of seeing Fred and George’s jealous faces| - | - | 
|protest.| - | - | 
|in protest.| - | - | 
|horizon,| - | - | 
|knuckles| - | - | 
|metal| - | - | 
|thick| - | - | 
|reached the end of its tether.| - | - | 
|Artefacts| - | - | 
|blurted out.| - | - | 
|gaped| - | - | 
|I will be writing to both your families tonight.| - | - | 
|‘Can you believe our luck, though?’| - | - | 
|‘Skip the lecture,’| - | - | 
|people’ll be talking about that one for years!’| - | - | 
|nudged| - | - | 
|‘I know I shouldn’t’ve enjoyed that or anything, but –’| - | - | 
|dashed| - | - | 

我想提取如下句子:

  1. 当她爬回车里时,
  2. 当她爬回车里时,他们已经很晚了,
  3. 推着他的手推车
  4. ‘我们被困住了,
  5. 还有可能看到弗雷德和乔治嫉妒的脸
  6. 已达到极限。
  7. 今晚我会写信给你们双方的家人。
  8. “不过,你能相信我们的运气吗?”
  9. “跳过讲座”,
  10. 人们会谈论这个多年!’
  11. “我知道我不应该喜欢那个或任何东西,但是 –”

我在 regex101 中尝试这样做网站,但实际上每次都匹配。

谁能帮帮我?

请您参考如下方法:

试试这个:

^\|[^\w\|]*(\w+\s+(?=\w+)[^\|]*) 

Explanation

  1. ^\| 如果该行以竖线 (|) 开头则匹配
  2. [^\w\|]* 抓取任何不在 a-z0-9 和 |
  3. 中的东西
  4. \w+\s+ 确保其后跟一个词和一个或多个 空白
  5. (?=\w+) 然后检查后面是否还有更多的词
  6. [^\|]* 如果找到先前的条件则抓取任何东西直到 下一个管道 |

对于每场比赛,第 1 组包含您想要的句子

Run the Code Sample here