Python find text in file between quotation marks
Question: I'm trying to capture the text within quotation marks and set them as
variables for me to change them at a later stage. I know how to do this in
bash shell, but I'm at a loss how to do this in Python.

I've started with this, but I was hoping someone can point me where my
mistakes are.

    
    
    import re
    input = open(filename, 'r')
    quotes = re.findall(r'"[^"]*"', input.read(), re.U)
    print quotes
    

Sadly though, this outputs:

    
    
    ['"test1"', '"test2"']
    

while I'm looking for:

    
    
    value1 = test1
    value2 = test2
    

In Bash I used this (but I obviously cant use it this way!):

    
    
    i=0
    regex='"([^"]*)"'
    while read line
    do
        if [[ $line =~ $regex ]]; then
            printf -v "text$i" '%s' "${BASH_REMATCH[1]}"
            i=$((i + 1))
        fi
    done < filename
    
    echo "value1: $text0"
    echo "value2: $text1"
    


Answer: Use a non-capturing group `(?:...)`, like this:

    
    
    In [18]: re.findall('(?:")([^"]*)(?:")', '''hello "foo" "bar" haha''')
    Out[18]: ['foo', 'bar']
    

Or use non-consuming groups `(?<=...)` etc:

    
    
    In [14]: re.findall('(?<=")[^"]*(?=")', '''hello "foo" "bar" haha''')
    Out[14]: ['foo', ' ', 'bar']
    

The latter has a side-effect of also selecting `" "` between `"foo"` and
`"bar"`.

