r"""
    Replaces text between sentinal lines in a block of text.

    Args:
        text (str):
        repl_ (str):
        start_tag (str):
        end_tag (str): (default=None)

    Returns:
        str: new_text

    CommandLine:
        python -m utool.util_str --exec-replace_between_tags

    Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_str import *  # NOQA
        >>> text = ut.codeblock(
            '''
            class:
                # <FOO>
                bar
                # </FOO>
                baz
            ''')
        >>> repl_ = 'spam'
        >>> start_tag = '# <FOO>'
        >>> end_tag = '# </FOO>'
        >>> new_text = replace_between_tags(text, repl_, start_tag, end_tag)
        >>> result = ('new_text =\n%s' % (str(new_text),))
        >>> print(result)
        new_text =
        class:
            # <FOO>
        spam
            # </FOO>
            baz

def replace_between_tags(text, repl_, start_tag, end_tag=None):
    r"""
    Replaces text between sentinal lines in a block of text.

    Args:
        text (str):
        repl_ (str):
        start_tag (str):
        end_tag (str): (default=None)

    Returns:
        str: new_text

    CommandLine:
        python -m utool.util_str --exec-replace_between_tags

    Example:
        >>> # DISABLE_DOCTEST
        >>> from utool.util_str import *  # NOQA
        >>> text = ut.codeblock(
            '''
            class:
                # <FOO>
                bar
                # </FOO>
                baz
            ''')
        >>> repl_ = 'spam'
        >>> start_tag = '# <FOO>'
        >>> end_tag = '# </FOO>'
        >>> new_text = replace_between_tags(text, repl_, start_tag, end_tag)
        >>> result = ('new_text =\n%s' % (str(new_text),))
        >>> print(result)
        new_text =
        class:
            # <FOO>
        spam
            # </FOO>
            baz
    """
    new_lines = []
    editing = False
    lines = text.split('\n')
    for line in lines:
        if not editing:
            new_lines.append(line)
        if line.strip().startswith(start_tag):
            new_lines.append(repl_)
            editing = True
        if end_tag is not None and line.strip().startswith(end_tag):
            editing = False
            new_lines.append(line)
    new_text = '\n'.join(new_lines)
    return new_text