import re


def re_soap_tag(text, tag, limit=4000, pattern=r"<{t}>(.*?)</{t}>"):
    """Search for tag in text[:limit] using pattern.

    Args:
        text (:obj:`str`):
            Text to search for pattern.
        tag (:obj:`str`):
            Tag name to use in pattern as 't'.
        limit (:obj:`int`, optional):
            Length to limit text to when searching for pattern.

            Defaults to: 4000.
        pattern (:obj:`str`, optional):
            Pattern to use when searching for tag.

            Defaults to: r'<{e}>(.*?)</{e}>'

    Notes:
        Given text is 4 GB and pattern is expected at top of text:
            * if head is None and pattern not found: 131 seconds
            * if head is None and pattern found: 0 seconds
            * if head is 4000 and pattern not found: 0 seconds
            * if head is 4000 and pattern found: 0 seconds

    Returns:
        :obj:`str`

    """
    pattern_txt = pattern.format(t=tag)
    pattern_re = re.compile(pattern_txt, re.IGNORECASE | re.DOTALL)
    text_limit = text[:limit]
    match = pattern_re.search(text_limit)
    return match.group(1) if match else ""