James Ch
James Ch

Reputation: 25

Break a line after multiple matched words - python

I have a line of words: Dane osobowe po zmianie (dopisane): Surname1 Name1 (RYSZARD, EUGENIA) Zam. 99-100 Muczyn (Geod) 6A, Płeć: K, PESEL: 99945104321 Jednostki Rejestrowe osoby (w gminie): 0001.G16 0002.G56 Surname2 Name2 (ANDRZEJ, AGNIESZKA) Zam. 99-100 Muczyn (Geod) 4A, Płeć: K Jednostki Rejestrowe osoby (w gminie): 0001.G16 0001.G07 0001.G43 Surname1 Name1 Name2 (ANDRZEJ, AGNIESZKA) Zam. 99-100 MUCZYN (Geod) 4A, Płeć: M Jednostki Rejestrowe osoby (w gminie): 0001.G16

I want to break into multiple lines in certain pattern:

Dane osobowe po zmianie (dopisane): Surname1 Name1 (RYSZARD, EUGENIA) Zam. 99-100 Muczyn (Geod) 6A, Płeć: K, PESEL: 99945104321

Jednostki Rejestrowe osoby (w gminie): 0001.G16 0002.G56

Surname2 Name2 (ANDRZEJ, AGNIESZKA) Zam. 99-100 Muczyn (Geod) 4A, Płeć: K

Jednostki Rejestrowe osoby (w gminie): 0001.G16 0001.G07 0001.G43

Surname1 Name1 Name2 (ANDRZEJ, AGNIESZKA) Zam. 99-100 MUCZYN (Geod) 4A, Płeć: M

Jednostki Rejestrowe osoby (w gminie): 0001.G16

I have a code:

for i in range(0, len(data)): data[i] = data[i].replace(' Jednostki', '\nJednostki')

in data I have single line as mentioned above. With this I managed to break it like that:

Dane osobowe po zmianie (dopisane): Surname1 Name1 (RYSZARD, EUGENIA) Zam. 99-100 Muczyn (Geod) 6A, Płeć: K, PESEL: 99945104321

Jednostki Rejestrowe osoby (w gminie): 0001.G16 0002.G56 Surname2 Name2 (ANDRZEJ, AGNIESZKA) Zam. 99-100 Muczyn (Geod) 4A, Płeć: K

Jednostki Rejestrowe osoby (w gminie): 0001.G16 0001.G07 0001.G43 Surname1 Name1 Name2 (ANDRZEJ, AGNIESZKA) Zam. 99-100 MUCZYN (Geod) 4A, Płeć: M

Jednostki Rejestrowe osoby (w gminie): 0001.G16

So, the problem is that I wan't it to break further after 0001.G16 0002.G56 or Jednostki Rejestrowe osoby (w gminie): 0001.G16 0001.G07 0001.G43

There's probably regex needed but how do I deal with that number of 0001.G** is changing.

Cheers!

Upvotes: 0

Views: 69

Answers (1)

j4nw
j4nw

Reputation: 2405

Match [0-9]{4}.G[0-9]{2}(?: [0-9]{4}.G[0-9]{2})*

Replace with \0\n

https://regex101.com/r/5sfaeL/2

Both operations with a single regexp:

Match ([0-9]{4}.G[0-9]{2}(?: [0-9]{4}.G[0-9]{2})* |( )(?=Jednostki))

https://regex101.com/r/5sfaeL/3

regex101 also provides an explanation of what's going on.

Upvotes: 1

Related Questions