import re def remove_disclaimer(text): # The pattern matches the start and end of your phrase. # \s+ matches one or more spaces/newlines/tabs. # re.IGNORECASE makes it case insensitive just in case. pattern = r"Caution:\s+This\s+email\s+comes\s+from\s+an\s+external\s+sender.*?\s+contact\s+your\s+IT\s+Department\.?" # Substitute the found pattern with an empty string cleaned_text = re.sub(pattern, "", text, flags=re.IGNORECASE | re.DOTALL) return cleaned_text.strip()