import re


def get_columns_matching(dataframe, pattern, regex=False):
    """
    Returns the columns containing pattern (or satisfying the regex pattern, if regex=True).

    :param dataframe: Dataframe whose columns are being tested
    :param pattern: String to test columns for
    :param regex: If True, then check pattern as regex
    :return: Dataframe slice of matching columns
    """
    if not regex:
        matching = [col for col in dataframe.columns.values.tolist() if pattern in col]
    else:
        matching = [col for col in dataframe.columns.values.tolist() if re.match(pattern, col)]
    return dataframe.loc[:, matching]