Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Regular Expressions to extract date

Tags:

python

regex

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?

like image 597
tgxiii Avatar asked Jun 29 '11 17:06

tgxiii


People also ask

How do I extract only the date from a string in Python?

Python provides the strptime() method, in its datetime class, to convert a string representation of the​ date/time into a date object.


1 Answers

'(\{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]+)\}'.

like image 106
Roman Bodnarchuk Avatar answered Sep 18 '22 08:09

Roman Bodnarchuk