Get string between double quotes python

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.

user2864740

58.1k14 gold badges134 silver badges208 bronze badges

asked Mar 29, 2014 at 18:56

1

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']

answered Mar 29, 2014 at 18:57

Martijn PietersMartijn Pieters

986k274 gold badges3877 silver badges3236 bronze badges

4

Use this one if your input can have something like this: some "text \" and text" more

s = '''According to some, dreams express "profound aspects of personality" [Foulkes 184], though others disagree.'''
lst = re.findall[r'"[.*?][?

answered Mar 29, 2014 at 18:59

Sabuj HassanSabuj Hassan

37.2k12 gold badges73 silver badges84 bronze badges

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we will learn to extract strings in between the quotations using Python.

    Method 1:

    To extract strings in between the quotations we can use findall[] method from re library.

    Python3

    import re

    inputstring = ' some strings are present in between "geeks" "for" "geeks" '

    print[re.findall['"[[^"]*]"', inputstring]]

    Output:

    ['geeks', 'for', 'geeks']

    Method 2:

    We can extract strings in between the quotations using split[] method and slicing.

    Python3

    inputstring = 'some strings are present in between "geeks" "for" "geeks" '

    result = inputstring.split['"'][1::2]

    print[result];

    Output:

    ['geeks', 'for', 'geeks']

    Here you can learn more about Regular Expressions and list slicing in python.

    Method 3: Using startswith[],endswith[] and replace[] methods

    Python3

    inputstring = 'some strings are present in between "geeks" "for" "geeks" '

    result = inputstring.split[]

    res=[]

    for i in result:

        if[i.startswith[]]:

            i=i.replace['"',""]

            res.append[i]

    print[res]

    Output

    ['geeks', 'for', 'geeks']


    How do you find the string between two double quotes in Python?

    Use the re. findall[] method to extract strings between quotes, e.g. my_list = re. findall[r'"[[^"]*]"', my_str] .

    How do I extract a substring between two markers in Python?

    Use indexing to get substring between two markers.
    s = "abcacbAUG|GAC|UGAfjdalfd".
    start = s. find["AUG|"] + len["AUG|"].
    end = s. find["|UGA"].
    substring = s[start:end].
    print[substring].

    How do you extract a string inside a single quote Python script?

    To extract strings in between the quotations we can use findall[] method from re library.

    How do you find the index of a double quote in a string in Python?

    index[] to find where the quotes[""] begin and end? temp. index['"'] , or temp. index["\""] .

    Chủ Đề