Question: I'm struggling to find a way to split a corpus of legal documents I have, by section. I've been trying to do this with regex, and while I've come reasonably close, I'm looking to see if there's a way I can refine the output even more to consolidate the number of matches that result from the regex script. Each document is organized with multiple section headers, but all follow the same basic structure. First, there is an "Argument" header summarizing the points made in each of the subsections. I want to include this Argument section because a handful of documents in the corpus have no subsequent subsections; however, the vast majority of them do have these sections. Each subsection begins with roman numerals, and the number of subsections in each document can vary. While I don't know exactly how many subsections are in each document, I'm assuming no more than 10. For the modal document, the structure looks something like: string = """ARGUMENT Summary of argument I. TITLE OF SUBSECTION 1 Text of subsection 1 II. TITLE OF SUBSECTION 2 Text of subsection 2 CONCLUSION Text of conclusion """  I created a regex script to try and split each section by its title using re.split, designating the ARGUMENT header, roman numeral subsections 1 (I) through 10 (X), and the CONCLUSION section, adding new line symbols to avoid splitting on every instance of those words/symbols, regardless of whether they happen to be in the titles themselves: r'(\nARGUMENT|\nI\.|\nII\.|\nIII\.|\nIV\.|\nV\.|\nVI\.|\nVII\.|\nVIII\.|\nIX\.|\nX\.|\nCONCLUSION.*)'  The output I want is a list where each title and the resulting text underneath are combined into one element, like below: ['ARGUMENT Summary of argument', 'I. TITLE OF SUBSECTION 1 Text of subsection 1', 'II. TITLE OF SUBSECTION 2 Text of subsection 2', 'CONCLUSION Text of conclusion']  However, when using re.split on the above string, my actual output separates the roman numerals from the rest of the text for that section (note the second and fourth elements of the list below: ['ARGUMENT\nSummary of argument\n', '\nI.', ' TITLE OF SUBSECTION 1\nText of subsection 1\n', '\nII.', ' TITLE OF SUBSECTION 2\nText of subsection 2\n', '\nCONCLUSION', '\nText of conclusion\n']  The newline symbols in the output are not particularly important to me. Rather, the consolidation of the header and the text underneath it is what matters to me more than anything. Is there some edit I can make to my regex script to get the first output as opposed to the second? Or if not, is there some other regex command that I could use to attain that particular output? And, less critically, is there a more efficient or streamlined way to match section headers with roman numerals I through X in my script?

Answer: The pattern is just missing some instructions on where and what to split at the patterns you provided. I wrote this pattern that gives more instructions on where to split the string, and remove the roman numeral redundancy. string = """ legal doc """ pattern = r'(?=\b(?:[IVXL]+\.)|CONCLUSION)' answer = re.split(pattern, string) answer = [line.replace('\n', ' ').strip() for line in answer] print(answer)  Assuming the legal docs will follow the same structure, we can ignore the ARGUMENT portion and split the text starting at the first point. pattern = r'(?=\b(?:[IVXL]+\.)|CONCLUSION)' So, at ?= we are including and looking ahead for the desired pattern. \b is the boundary for the word, it will help to keep larger roman numerals from splitting up. ?: is non-capturing to prevent redundant elements in the final answer. In [IVXL]+ we are asking to find these characters that may repeat, then using \. we require the roman numeral to end with a period. CONCLUSION simply splits the final paragraph and its contents. answer = [line.replace('\n', ' ').strip() for line in answer] This line is comprehension to remove those newline symbols and also strips whitespaces to help with cleanup. Hope this helps!