提取双引号之间的字符串 [英] Extract a string between double quotes

查看:65
本文介绍了提取双引号之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读来自期刊或论文来源的回复,我的 html 回复是一个字符串,例如:

<块引用>

根据一些人的说法,梦表达了人格的深刻方面"(Foulkes 184),尽管其他人不同意.

我的目标只是从给定的字符串中提取所有引号并将它们保存到列表中.我的方法是:

[match.start() for m in re.Matches(inputString, "\"([^\"]*)\""))]

不知何故,它对我不起作用.对我的正则表达式有什么帮助吗?非常感谢.

解决方案

前提是没有嵌套引号:

re.findall(r'"([^"]*)"', inputString)

演示:

<预><代码>>>>进口重新>>>inputString = '根据一些人的说法,梦表达了人格的深刻方面"(Foulkes 184),尽管其他人不同意.>>>re.findall(r'"([^"]*)"', inputString)['人格的深刻方面']

I'm reading a response from a source which is an journal or an essay and I have the html response as a string like:

According to some, dreams express "profound aspects of personality" (Foulkes 184), though others disagree.

My goal is just to extract all of the quotes out of the given string and save each of them into a list. My approach was:

[match.start() for m in re.Matches(inputString, "\"([^\"]*)\""))]

Somehow it didn't work for me. Any helps on my regex here? Thanks a lot.

解决方案

Provided there are no nested quotes:

re.findall(r'"([^"]*)"', inputString)

Demo:

>>> import re
>>> inputString = 'According to some, dreams express "profound aspects of personality" (Foulkes 184), though others disagree.'
>>> re.findall(r'"([^"]*)"', inputString)
['profound aspects of personality']

这篇关于提取双引号之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆