Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract a string between double quotes

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.

like image 855
Kiddo Avatar asked Mar 29 '14 18:03

Kiddo


People also ask

How do I convert a string between two quotes in Excel?

Another way to supply a double quote (") to an Excel formula is by using the CHAR function with the code number 34. The first two arguments of this MID formula raise no questions: A2 is the text string to search, and. SEARCH("""", A2) +1 is the starting number, i.e. the position of the first quote +1.

How do you escape quotation marks in a string?

You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string. Here is an example. The backslashes protect the quotes, but are not printed.

How do you split quotation marks?

Question marks and exclamation marks go inside the quotation marks when they are part of the original quotation. For split quotations, it's also necessary to add a comma after the first part of the quotation and after the narrative element (just like you would with a declarative quotation).


1 Answers

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']
like image 192
Martijn Pieters Avatar answered Oct 04 '22 04:10

Martijn Pieters