import re


def _split_text_by_opening(pattern, text):
    """
    Splits text into parts identified by opening that matches `pattern`.
    For example, --pattern='\n\nCHAPTER \\d+\n\n' may be used
    to split text into chapters.
    """
    openings = re.findall(pattern, text)

    if len(openings) == 0:
        print(f'\n❗ No text matching pattern "{pattern}". Splitting is not performed.\n')
        return []

    texts = re.split(pattern, text)
    texts = [d + t for d, t in zip(openings, texts[1:])]
    return texts