如何使用Python或regex获得引号中的对话行

如何使用Python或regex获得引号中的对话行

我在这个网站上尝试了一些答案,但是没有运气。这是我正在处理的文本类型的示例:


  伊丽莎白说:“但是如果今天有他们,我母亲会
  目的会得到解决。”
  
  她终于向父亲勒索,承认
  马匹订婚了。因此,简不得不骑马。
  她妈妈带着很多乐观的预言陪着她到门口
  糟糕的一天。她的希望得到了满足;简没走多久
  在下大雨之前。她的姐妹们为她感到不安,但她的母亲
  很高兴。整个晚上持续下雨
  间歇简当然不能回来。
  
  “的确,这是我的幸运主意!” Bennet太太说
  曾经,好像下雨的功劳都是她自己的。直到
  然而,第二天早上,她并不知道自己的喜好
  努力。当一个仆人从
  尼瑟菲尔德为伊丽莎白带来了以下注意事项:
  
  “我的痴迷-
  
  “我发现自己今天早上非常不适,我想这是
  归因于我昨天湿透了。我的好朋友不会
  听到我的归来,直到我好起来。他们也坚持我的见解
  琼斯先生-因此,如果您听到他的消息,不要惊慌
  曾经来过我-除了喉咙痛和头痛之外,还有
  我没什么大不了的。--你们等等。”
  
  “好吧,亲爱的,”当伊丽莎白读完便条时,班纳特先生说。
  大声说:“如果您的女儿应该患有危险的疾病-如果
  她应该死,知道这一切都在里面真是令人欣慰
  在您的命令下追捕宾利先生。”
  
  “哦!我不怕她死。人们不会死于小事
  感冒。她会得到很好的照顾。只要她留下
  在那里,一切都很好。如果可以的话,我去看她。
  运输。”


我想从这个例子中提取

"But if you have got them to-day, my mother's purpose will be answered"
"This was a lucky idea of mine, indeed!"
"MY DEAREST LIZZY,-- I find myself very unwell this morning, which, I suppose, is to be imputed to my getting wet through yesterday. My kind friends will not hear of my returning till I am better. They insist also on my seeing Mr. Jones--therefore do not be alarmed if you should hear of his having been to me--and, excepting a sore throat and headache, there is not much the matter with me.--Yours, etc."
"Well, my dear,"


...等等。我试图进入正则表达式的规则是

1. get all strings within a " " (there can be multiple on the same line)
2. if the line ends with a \n before finding a second ", continue grabbing the next line so long as it also begins with a "

最佳答案

它可能不是您想要的,但是您可以尝试以下操作:RegexDemo

text = '''
"But if you have got them to-day," said Elizabeth, "my mother's purpose will be answered."

She did at last extort from her father an acknowledgment that the horses were engaged. Jane was therefore obliged to go on horseback, and her mother attended her to the door with many cheerful prognostics of a bad day. Her hopes were answered; Jane had not been gone long before it rained hard. Her sisters were uneasy for her, but her mother was delighted. The rain continued the whole evening without intermission; Jane certainly could not come back.

"This was a lucky idea of mine, indeed!" said Mrs. Bennet more than once, as if the credit of making it rain were all her own. Till the next morning, however, she was not aware of all the felicity of her contrivance. Breakfast was scarcely over when a servant from Netherfield brought the following note for Elizabeth:

"MY DEAREST LIZZY,--

"I find myself very unwell this morning, which, I suppose, is to be imputed to my getting wet through yesterday. My kind friends will not hear of my returning till I am better. They insist also on my seeing Mr. Jones--therefore do not be alarmed if you should hear of his having been to me--and, excepting a sore throat and headache, there is not much the matter with me.--Yours, etc."

"Well, my dear," said Mr. Bennet, when Elizabeth had read the note aloud, "if your daughter should have a dangerous fit of illness--if she should die, it would be a comfort to know that it was all in pursuit of Mr. Bingley, and under your orders."

"Oh! I am not afraid of her dying. People do not die of little trifling colds. She will be taken good care of. As long as she stays there, it is all very well. I would go and see her if I could have the carriage."
'''

talk = re.findall(r'\"([^\"]+?)(\"|\-\-\n)',text)
for t in talk:
    print(t[0])

关于python - 如何使用Python或regex获得引号中的对话行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55720450/

10-11 10:21