import re


def filter_output(output, regex):
    """Filter output by defined regex. Output can be either string, list or tuple.
       Every string is split into list line by line. After that regex is applied
       to filter only matching lines, which are returned back.

       :returns: list of matching records
    """
    result = []
    if isinstance(output, str):
        for line in output.splitlines():
            result += re.findall(regex, line)
    elif isinstance(output, (list, tuple)):
        for item in output:
            result.append(filter_output(item, regex))
    else:
        raise RuntimeError('Only strings and lists are supported by filter_output(), '
                           'but output has type {}'.format(type(output)))
    return result