gpt4 book ai didi

python - 使用正则表达式从文本文件中提取字符串

转载 作者:太空宇宙 更新时间:2023-11-04 01:03:01 25 4
gpt4 key购买 nike

基本上我有一个 txt 文档,里面有这个,

The sound of a horse at a gallop came fast and furiously up the hill.
"So-ho!" the guard sang out, as loud as he could roar.
"Yo there! Stand! I shall fire!"
The pace was suddenly checked, and, with much splashing and floundering, a man's voice called from the mist, "Is that the Dover mail?"
"Never you mind what it is!" the guard retorted. "What are you?"
"_Is_ that the Dover mail?"
"Why do you want to know?"
"I want a passenger, if it is."
"What passenger?"
"Mr. Jarvis Lorry."
Our booked passenger showed in a moment that it was his name.
The guard, the coachman, and the two other passengers eyed him distrustfully.

使用正则表达式我需要在双引号内打印所有内容,我不想要完整的代码我只需要知道我应该如何去做,哪个正则表达式最有用。请提示和指点!

最佳答案

r'(".*?")' 将匹配双引号内的每个字符串。括号表示捕获的组,. 匹配每个字符(换行符除外),* 表示重复,? 表示重复非贪婪(在下一个双引号之前停止匹配)。如果需要,请包含 re.DOTALL 选项以使 . 也匹配换行符。

关于python - 使用正则表达式从文本文件中提取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31954016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com