2014-05-08

Find all words within quotation chars with Python

Today I've spent about an hour googling everywhere to find out how to get all words within quotes "" from the text.
And here is my result:

import re

def match_quotes(s):
    return re.findall(r"\"(.*?)\"", s)

if __name__ == "__main__":
    print match_quotes('Example of "quotation string" with several "quotes"')


Another one regex issue is to find a part of the string from the certain char till the end:

import re

def match_end_of_the_string(s, c):
    return re.findall(r"%s(.*?)$"%c, s)

if __name__ == "__main__":
    print match_end_of_the_string('Example of #commented string', "#")
Certainly, it's less obvious than split() approach. :)