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, 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?
Python provides the strptime() method, in its datetime class, to convert a string representation of the date/time into a date object.
'(\{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]+)\}'
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With