Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for multiple occurances in python

Tags:

python

regex

I need to parse lines having multiple language codes as below

008800002     Bruxelles-Nord$Br�ussel Nord$<deu>$Brussel Noord$<nld>
  • 008800002 being a id
  • Bruxelles-Nord$Br�ussel Nord$ being name1
  • deu being language one
  • $Brussel Noord$ being name two
  • nld being language two.

SO, the idea is name and language can appear N number of times. I need to collect them all. the language in <> is 3 characters in length (fixed) and all names end with $ sign.

I tried this one but it is not giving expected output.

x = re.compile('(?P<stop_id>\d{9})\s(?P<authority>[[\x00-\x7F]{3}|\s{3}])\s(?P<stop_name>.*)
    (?P<lang_code>(?:[<]\S{0,4}))',flags=re.UNICODE)

I have no idea how to get repeated elements. It takes

Bruxelles-Nord$Br�ussel Nord$<deu>$Brussel Noord$ as stop_name and <nld> as language.

like image 579
Ishan Bhatt Avatar asked Mar 16 '26 11:03

Ishan Bhatt


1 Answers

Do it in two steps. First separate ID from name/language pairs; then use re.finditer on the name/language section to iterate over the pairs and stuff them into a dict.

import re

line = u"008800002     Bruxelles-Nord$Br�ussel Nord$<deu>$Brussel Noord$<nld>"
m = re.search("(\d+)\s+(.*)", line, re.UNICODE)
id = m.group(1)
names = {}
for m in re.finditer("(.*?)<(.*?)>", m.group(2), re.UNICODE):
    names[m.group(2)] = m.group(1)
print id, names
like image 78
Amadan Avatar answered Mar 19 '26 01:03

Amadan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!