How can i find the string inside single quotes in python regex
Question: I want to match the string inside these lines

    
    
    key: value
    key: 'value'
    

I am using this regex

`re.compile(r"(.*key: )(.*)")`

but \2 always catches single quotes as well.

I tried many things like

`(.*key: )'?(.*)'?` but didn't work

I am trying like this

    
    
    line = regex.sub(r"\1'blah'\2", line)
    


Answer: I think you are trying to capture the value which was not present within the
single quotes aswell as the one present within the single quotes.

    
    
    key:\s*'?([^'\n]*)'?
    

Group index 1 contains the value of the field `key`.

[DEMO](http://regex101.com/r/bX3wF8/1)

    
    
    >>> import re
    >>> s = """key: value
    ... key: 'value'"""
    >>> m = re.findall(r"key:\s*'?([^'\n]*)'?", s, re.M)
    >>> m
    ['value', 'value']
    

