formato de tiempo de coincidencia de python desde re

In the following string, how can I write a regular expression with respect to time. The output should be the timestamps only.

 l1=May 30, 2012 at 8:13 AM  Comment · 1Like Unlike · Bookmark Unbookmark
 l2=yesterday at 12:13 AM  2Comment  Like Unlike · Bookmark Unbookmark
 l3=Two days ago at 01:18 AM  Comment · 5Like Unlike · Bookmark Unbookmark
 l4=Two days ago at 15:54 PM  Comment · Like Unlike · Bookmark Unbookmark

EDIT

 l5=Two days ago at 15:54:51 PM · Comment · Like Unlike · Bookmark Unbookmark

Salida:

 array1 = [May 30, 2012 at 8:13 AM ,yesterday at 12:13 AM ,Two days ago at 01:18 AM,Two days ago at 15:54 PM]

 array2=[Comment · 1Like Unlike · Bookmark Unbookmark,2Comment · Like Unlike · Bookmark Unbookmark,Comment · 5Like Unlike · Bookmark Unbookmark,Comment · Like Unlike · Bookmark Unbookmark]

EDIT:

p_date = re.compile(r'(\d{1,2}[:]\d{1,2}) but i wasnt sure how to do it if the timestamp was also like 23:12:29 

preguntado el 04 de julio de 12 a las 09:07

This is not valid Python. Please show some effort. -

p_date = re.compile(r'(\d{1,2}[:]\d{1,2}) but i wasnt sure how to do it if the timestamp was also like 23:12:29 -

2 Respuestas

>>> import re
>>> pattern = r'l\d+=(.*?)·(.*)'
>>> l1 = []
>>> l2 = []
>>> for line in s.split('\n'):
    m = re.match(pattern, line)
    if m:
        l1.append(m.groups()[0])
        l2.append(m.groups()[1])


>>> l1
['May 30, 2012 at 8:13 AM ', 'yesterday at 12:13 AM ', 'Two days ago at 01:18 AM ', 'Two days ago at 15:54 PM ']
>>> l2
[' Comment \xb7 1Like Unlike \xb7 Bookmark Unbookmark', ' 2Comment \xb7 Like Unlike \xb7 Bookmark Unbookmark', ' Comment \xb7 5Like Unlike \xb7 Bookmark Unbookmark', ' Comment \xb7 Like Unlike \xb7 Bookmark Unbookmark']
>>> 

EDITAR : added the matching of l1= to remove it from matching.

Respondido 04 Jul 12, 09:07

What if also i have timestamps like 23:31:39 ?? - Rajeev

@Rajeev: Have you actually tried his solution? Why are you not happy with it? - Tim Pietzcker

No problem as long as it's between = y ·. - Emmanuel

@Rajeev: Yes it can. At least in your examples, it can. So please correct your question. It's still invalid Python, and it's useless trying to answer a question when it's not even clear what the actual question is. - Tim Pietzcker

Yes i have removed the '.' that was separating the text. - Rajeev

You can split your output by "." , if your input format is consistent. Applying regular expressions to identify differently formed timestamp strings may be hectic task.

Respondido 04 Jul 12, 09:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.