import re def extract_steps(text, steps=[2, 4, 5]): """ Extracts the specified steps from the text. Args: text (str): The input text containing various steps. steps (list of int): The step numbers to extract. Returns: str: A new string with each specified step's content on its own line. """ step_pattern = "|".join([f"Step {step}\." for step in steps]) matches = re.findall( f"({step_pattern})\s*(.*?)\s*(?=(Step \d\.|$))", text, re.DOTALL ) # Extract and join the matched content, skipping the "Step n." part extracted_text = "\n".join(match[1].strip() for match in matches) return extracted_text