2

I have a string:

05-01-2015 12:27 - KH - (KH) Igangværende - Opringning - 13-11 00:00 Fangede RLI på hans mobil. Ring igen kl. 15 19-11-2014 11:17 - KH - (KH) Igangværende - Opringning - 13-11 00:00 Gik på svarer igen og lagt besked til RLI at ringe tilbage. 12-11-2014 09:38 - KH - (KH) Igangværende - Opringning - 13-11 00:00 12-11-2014 09:32 - KH - (KH) Igangværende - Opringning - 15-10 00:00 Forsøgt RLI igen og lagt besked om han vil ringe. 14-10-2014 13:14 - KH - (KH) Igangværende - Opringning - 15-10 00:00 14-10-2014 13:10 - KH - (KH) Igangværende - Opringning - 14-10 00:00 Lagt besked til RLI at ringe 14-10-2014 13:06 - KH - (KH) Igangværende - Opringning - 14-10 00:00 test

I need to parse this string into pieces so that each piece starts with dates. For this purpose, I tried to benefit from regex like :

match = re.search(r'\d{2}-\d{2}-\d{4}', text)

But this code only finds dates. And I cant go further. I need to have pieces such as:

first_piece: 05-01-2015 12:27 - KH - (KH) Igangværende - Opringning - 13-11 00:00 Fangede RLI på hans mobil. Ring igen kl. 15

second_piece: 19-11-2014 11:17 - KH - (KH) Igangværende - Opringning - 13-11 00:00 Gik på svarer igen og lagt besked til RLI at ringe tilbage.

and so on.

Could you please give me some insights about achieving these sub strings?

Thanks in advance.

Şansal Birbaş
  • 443
  • 1
  • 6
  • 14

3 Answers3

5

Does this work?

re.split(r' (?=\d{2}-\d{2}-\d{4})', text)
  • Thanks marcus. İts great – Şansal Birbaş Jun 23 '15 at 20:26
  • When i get the text from excel cell with xlrd or other, I cant parse the text. It comes as a whole. What can it cause that? I even tried encode/decode things. I mean, in above case, match[0] gives whole text, and match[1] or others dont exist. In normal case that i put text directly into this code, there is no problem. – Şansal Birbaş Jun 23 '15 at 21:59
  • it is posted here http://stackoverflow.com/questions/31014673/re-split-doesnt-work-properly-with-a-string-coming-from-excel-cell?noredirect=1#comment50056753_31014673 – Şansal Birbaş Jun 23 '15 at 23:18
2

Marcus has the right answer but there's a fun little detail that's missing from their answer.

Test file multiple_dates.py

import re

test_string = u"05-01-2015 12:27 - KH - (KH) Igangværende - Opringning - 13-11 00:00 Fangede RLI på hans mobil. Ring igen kl. 15 19-11-2014 11:17 - KH - (KH) Igangværende - Opringning - 13-11 00:00 Gik på svarer igen og lagt besked til RLI at ringe tilbage. 12-11-2014 09:38 - KH - (KH) Igangværende - Opringning - 13-11 00:00 12-11-2014 09:32 - KH - (KH) Igangværende - Opringning - 15-10 00:00 Forsøgt RLI igen og lagt besked om han vil ringe. 14-10-2014 13:14 - KH - (KH) Igangværende - Opringning - 15-10 00:00 14-10-2014 13:10 - KH - (KH) Igangværende - Opringning - 14-10 00:00 Lagt besked til RLI at ringe 14-10-2014 13:06 - KH - (KH) Igangværende - Opringning - 14-10 00:00 test"

groups = re.split(r' (?=\d{2}-\d{2}-\d{4})', test_string)

for group in groups:
    print(group)

If I run the given example in python2.7 I get

 python multipe_dates.py 
  File "multipe_dates.py", line 3
SyntaxError: Non-ASCII character '\xc3' in file multipe_dates.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

If I run this with python3 it works by default

python3 multipe_dates.py 
05-01-2015 12:27 - KH - (KH) Igangværende - Opringning - 13-11 00:00 Fangede RLI på hans mobil. Ring igen kl. 15
19-11-2014 11:17 - KH - (KH) Igangværende - Opringning - 13-11 00:00 Gik på svarer igen og lagt besked til RLI at ringe tilbage.
12-11-2014 09:38 - KH - (KH) Igangværende - Opringning - 13-11 00:00
12-11-2014 09:32 - KH - (KH) Igangværende - Opringning - 15-10 00:00 Forsøgt RLI igen og lagt besked om han vil ringe.
14-10-2014 13:14 - KH - (KH) Igangværende - Opringning - 15-10 00:00
14-10-2014 13:10 - KH - (KH) Igangværende - Opringning - 14-10 00:00 Lagt besked til RLI at ringe
14-10-2014 13:06 - KH - (KH) Igangværende - Opringning - 14-10 00:00 test

If you add

# -*- coding: utf-8 -*- 

to the top of your py file it'll work in python2

AlexLordThorsen
  • 8,057
  • 5
  • 48
  • 103
1

you could use this pattern

(\d\d-\d\d-\d{4}.*?)(?=\d\d-\d\d-\d{4}|$)

Demo

alpha bravo
  • 7,838
  • 1
  • 19
  • 23