Appending Multiple Regex Matches to a List of Lists

Viewed 85
txt = '0    Marriage of MARY ROCHE
1   in 1880
2   Group Registration ID\tN/R
3   SR District/Reg Area\tCork
4   Returns Year\t1880
5   Returns Quarter\t4
6   Returns Volume No\t5
7   Returns Page No\t0110
8   Marriage of MARY ROCHE
9   in 1880
10  Group Registration ID\tN/R
11  SR District/Reg Area\tEnniscorthy
12  Returns Year\t1880
13  Returns Quarter\t3
14  Returns Volume No\t4
15  Returns Page No\t276"

The above is a snippet of a Marriage Records Dataset. Each 8 lines correspond to a new woman's record. I'm attempting to extract the key details (year, area, quarter, volume, page no) through regex expressions.

 year = re.compile(r'in\s\d{4}')
area = re.compile(r'Area\t[A-Za-z]+(?:\s[A-Za-z]+)*$')
fdata = []
file = open('C:\\Downloads\\mary_roche.txt', 'r')
for line in file:
    year_matches = year.finditer(line)
    area_matches = area.finditer(line)
    for a in area_matches:
        for y in year_matches:
            fdata.append([y.group(),a.group()])
   
    
print(len(fdata))
print(fdata)

When I use these expressions individually, they work, but when I try to add the two to a list of lists, I get nothing. My end goal is to create expressions for all five key details and store them in an orderly fashion, i.e [[woman1], [woman2], [woman3]...etc]

Any help here is greatly appreciated. Cheers!

1 Answers

There are only two quirks in the logic of the for loop.

  • You tried the matches for each individual line, but since there's no single line which contains both area and year, you got nothing. Remedy: Just operate on the dataset as a whole.
  • You would iterate over all years for one area; this way, after the first found area all year matches would be consumed. Remedy: For each area, only get one year match.
area = re.compile(r'Area\t[A-Za-z]+(?:\s[A-Za-z]+)*$', re.M)
…
file = open('C:\\Downloads\\mary_roche.txt', 'r').read()
year_matches = year.finditer(file)
area_matches = area.finditer(file)
for a in area_matches:
    y = next(year_matches)
    fdata.append([y.group(), a.group()])

Note that we need the re.MULTILINE flag with $ since the file string now has multiple lines.

Of course we can shorten this and just write:

fdata = [*zip(year.findall(file), area.findall(file))]
Related