Python Regular Expressions to extract date
Question: I have strings that look like these:

    
    
    {server}_{date:YYYYMMDD}{int:######}
    {server}_{date:MON DAY YYYY}{int:######}
    

...plus more, in different date formats. Also, there can be any number of {}
blocks, and they can appear in any order.

I'm trying to get just the "date" part between the curly braces in Python 3.2.
So for the first string, I want to get just "{date:YYYYMMDD}" and for the
second string I want just "{date:MON DAY YYYY}". The only characters I want
inside the "date" block are alpha and whitespace.

My regex pattern is:

    
    
    \{date:(\w|\s)*\}
    

I've tested this out on [this Regex builder](http://gskinner.com/RegExr/ "this
Regex builder"), but it's not matching as expected. This is my output on
Python:

    
    
    >>> import re
    >>> re.findall('\{date:(\w|\s)*\}', '{server}_{date:YYYYMMDD}{date:MONDAYYYYY}{int:######}')
    ['D', 'Y']
    >>> re.findall('\{date:(\w|\s)*\}', '{server}_{date:MON DAY YYYY}{int:######}')
    ['Y']
    

Can someone please point out what's wrong with my pattern?


Answer: `'(\{date:[\w\s]+\})'` gives what you want:

    
    
    >>> import re
    >>> re.findall('(\{date:[\w\s]+\})', '{server}_{date:YYYYMMDD}{date:MONDAYYYYY}{int:######}')
    ['{date:YYYYMMDD}', '{date:MONDAYYYYY}']
    >>> re.findall('(\{date:[\w\s]+\})', '{server}_{date:MON DAY YYYY}{int:######}')
    ['{date:MON DAY YYYY}']
    

If you want only data value, use `'\{date:([\w\s]+)\}'`.

