Python 提取双引号之间的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22735440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract a string between double quotes
提问by Kiddo
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:
我正在阅读来自期刊或论文的来源的回复,我将 html 回复作为字符串,如下所示:
According to some, dreams express "profound aspects of personality" (Foulkes 184), though others disagree.
根据一些人的说法,梦表达了“人格的深刻方面”(Foulkes 184),尽管其他人不同意。
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.
不知何故,它对我不起作用。对我的正则表达式有什么帮助吗?非常感谢。
采纳答案by Martijn Pieters
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']
回答by Sabuj Hassan
Use this one if your input can have something like this: some "text \" and text" more
如果您的输入可以是这样的,请使用这个: some "text \" and text" more
s = '''According to some, dreams express "profound aspects of personality" (Foulkes 184), though others disagree.'''
lst = re.findall(r'"(.*?)(?<!\)"', s)
print lst
Using (?<!\\)negative lookbehind it is checking there is no \before the "
使用(?<!\\)负向后视检查它\之前没有"

