fork download
  1. import re
  2. text = "50. The SBI considered this sub-item at its resumed 1^st^ meeting and at its 2^nd^ meeting (see para. 46 above). It had before it documents FCCC/CP/2011/7 and Corr.1 and Add.1 and 2, FCCC/SBI/2010/17, FCCC/SBI/2010/26 and FCCC/SBI/2010/MISC.9. A statement was made on behalf of the LDCs."
  3. rx_main = re.compile(r'(FCCC/(?:SBSTA|SBI|CP|KP/CMP|PA/CMA)/\d{4}/(?:INF\.|L\.|MISC\.)?\d+)((?:(?:/|\s+and\s+|\s*,\s*)(?:Add|Rev|Corr)\.\d+(?:(?:\s*,\s*|\s+and\s+)\d+)*)*)')
  4. rx_rev = re.compile(r'(?:Add|Rev|Corr)\.\d+(?:(?:\s*,\s*|\s+and\s+)\d+)*')
  5. rx_split = re.compile(r'\s*,\s*|\s+and\s+')
  6. matches = rx_main.finditer(text)
  7. results = []
  8. for m in matches:
  9. results.append(m.group(1))
  10. chunks = [rx_split.split(x) for x in rx_rev.findall(m.group(2))]
  11. for ch in chunks:
  12. if len(ch) == 1: # it is simple, just add
  13. results.append(f"{m.group(1)}/{ch[0]}")
  14. else:
  15. name = ch[0].split('.')[0] # Rev, Corr or Add
  16. for c in ch:
  17. if '.' in c:
  18. results.append(f"{m.group(1)}/{c}")
  19. else:
  20. results.append(f"{m.group(1)}/{name}.{c}")
  21.  
  22. print(results)
Success #stdin #stdout 0.02s 9620KB
stdin
Standard input is empty
stdout
['FCCC/CP/2011/7', 'FCCC/CP/2011/7/Corr.1', 'FCCC/CP/2011/7/Add.1', 'FCCC/CP/2011/7/Add.2', 'FCCC/SBI/2010/17', 'FCCC/SBI/2010/26', 'FCCC/SBI/2010/MISC.9']