I’m doing some data analysis over the judge appointment system in France, and using some code to do so. But I’ve run into a regex problem.
My data, is made of a big list of courts and judges appointments.
Here’s an example list :
Appellate Court of Paris
Blabla about the jugdge : Mr Dominique Martin, blahblahblah Blabla
about the judge : Ms Jeanne TrucDistrict Court of Paris
Blabla about the jugdge : Mr François Dupont, blahblahblah Blabla
about the jugdge : Mr Jean Dupuis, blahblahblah Blabla about the
jugdge : Ms Marguerite Lorem, blahblahblahDistrict Court of Lyon
Blabla about the judge : Ms Lorena Ipsum, blablabh
And there a big lot of these, district and appellate courts mingled. Usually, after a city appellate court, comes the district one.
(For the french guys reading this, I’m translating “Tribunal de grande instance” to “District Court” and “Cour d’appel” to “Appellate Court” it’s easier to understand if everything is in English)
And then I need a list of the judges by the courts their in.
To achieve that, I started doing a regex using good ol’ python 3. First I get a list of all the judges thanks to the Mr and Mrs (well it’s french so M. and Mme. ). And then, iterating over each judge in this list to find where the hell their being appointed. I wrote something like that :
court = re.findall(r'(District Court of.+?|Appelate Court of.+?)\n.+?'+ judge, appointments, re.S)
The “judge” var is then the name of the current judge I’m seeking and “appointments” the list I’m regexing on. Dotall flag is on, and if you don’t know python, forget about the ‘r’ before the pattern, it’s just about how to interpret special chars in the string following.
(I’ve also tried using re.search, but i think re.findall is better for spotting bugs)
And the result is always the first one in the list (for instance : Appellate Court of Paris)… just as if it was working as a greedy request, even if I flagged every quatifier with the ‘?’ that in python identifies the quantifier as non-greedy.
(I’m a law student, not a CS one, I beg for forgiveness if I’m doing something utterly wrong there).
Note: The original list i’m parsing : http://www.legifrance.gouv.fr/affichTexte.do?cidTexte=JORFTEXT000022472292 (BEWARE that’s an UGLY french government website).
The problem with your current regex is that even with lazy quantifiers the match will occur as early in the string as the regex allows. For example
re.search('A+?B', 'AAB')would match the entire string ‘AAB’, it would not move on to the second ‘A’ so that it matches as few as possible.So basically, you get the name of a court and then search through the string until you find the first occurrence of the judge’s name, even if that means you are entering a new court section. Here are two good options for preventing this:
appointmentsat each new court, and then search each court for the judge name individually.or…
Here is an example with a negative lookahead:
Your current code would be similar to the following: