Beispiel #1
0
def list_filter(pattern, iter, flags=0):
    """Use regular expression patterns to filter a list of strings

    Args:
        pattern (str|list): String or list of strings of regular expression patterns.
        iter (list): List of strings to be filtered. Can be any iterable object that can be iterated in a for-loop.
        flags (int): See :py:mod:`re`

    Returns:
        list: List of matching strings.

    """
    if is_list(pattern):
        pass
    elif is_str(pattern):
        pattern = [pattern]
    else:
        raise TypeError("'pattern' must be a string or list of strings")

    if not is_list(iter):
        raise TypeError("'iter' must be a list of strings")

    re_filters = []
    for p in pattern:
        re_filters.append(re.compile(p, flags))

    matches = []
    for s in iter:
        for f in re_filters:
            if f.match(s):
                matches.append(s)
                break
    return matches
Beispiel #2
0
def list_filter(pattern, iter, flags=0):
    """Use regular expression patterns to filter a list of strings

    Args:
        pattern (str|list): String or list of strings of regular expression patterns.
        iter (list): List of strings to be filtered. Can be any iterable object that can be iterated in a for-loop.
        flags (int): See :py:mod:`re`

    Returns:
        list: List of matching strings.

    """
    if is_list(pattern):
        pass
    elif is_str(pattern):
        pattern = [pattern]
    else:
        raise TypeError("'pattern' must be a string or list of strings")

    if not is_list(iter):
        raise TypeError("'iter' must be a list of strings")

    re_filters = []
    for p in pattern:
        re_filters.append(re.compile(p, flags))

    matches = []
    for s in iter:
        for f in re_filters:
            if f.match(s):
                matches.append(s)
                break
    return matches
Beispiel #3
0
def str_split_caps(s, trim=True):
    """Split a string on capital letters

    Args:
        s (str): The string to be divided. Value type is converted to string.
        trim (bool): Remove whitespace. Defaults to True.

    Returns:
        list: List of strings, each starting with a capital letter, in the same order as they appear in the string.

    """
    if not is_str(s): raise TypeError("'s' must be a string")
    if not is_bool(trim): raise TypeError("'trim' must be True or False")

    result = re.findall('[A-Z][^A-Z]*', s)
    if trim:
        result_trim = []
        for w in result:
            result_trim.append(w.strip())
        result = result_trim
    return result
Beispiel #4
0
def str_split_caps(s, trim=True):
    """Split a string on capital letters

    Args:
        s (str): The string to be divided. Value type is converted to string.
        trim (bool): Remove whitespace. Defaults to True.

    Returns:
        list: List of strings, each starting with a capital letter, in the same order as they appear in the string.

    """
    if not is_str(s): raise TypeError("'s' must be a string")
    if not is_bool(trim): raise TypeError("'trim' must be True or False")

    result = re.findall('[A-Z][^A-Z]*', s)
    if trim:
        result_trim = []
        for w in result:
            result_trim.append(w.strip())
        result = result_trim
    return result