Extract string from between quotations Extract string from between quotations python python

Extract string from between quotations


>>> import re>>> re.findall('"([^"]*)"', 'SetVariables "a" "b" "c" ')['a', 'b', 'c']


You could do a string.split() on it. If the string is formatted properly with the quotation marks (i.e. even number of quotation marks), every odd value in the list will contain an element that is between quotation marks.

>>> s = 'SetVariables "a" "b" "c"';>>> l = s.split('"')[1::2]; # the [1::2] is a slicing which extracts odd values>>> print l;['a', 'b', 'c']>>> print l[2]; # to show you how to extract individual items from outputc

This is also a faster approach than regular expressions. With the timeit module, the speed of this code is around 4 times faster:

% python timeit.py -s 'import re' 're.findall("\"([^\"]*)\"", "SetVariables \"a\" \"b\" \"c\" ")'1000000 loops, best of 3: 2.37 usec per loop% python timeit.py '"SetVariables \"a\" \"b\" \"c\"".split("\"")[1::2];'1000000 loops, best of 3: 0.569 usec per loop


Regular expressions are good at this:

import requoted = re.compile('"[^"]*"')for value in quoted.findall(userInputtedText):    print value