(text, remove='')
Perform string.strip-like functions but using regexes.
Perform string.strip-like functions but using regexes.
Perform string . strip - like functions but using regexes .
def strip(text, remove=''):
    """Perform string.strip-like functions but using regexes."""
    # Removes whitespace from either end of the string
    if remove == '':
        space_regex = re.compile(r'^(\s*)(\S*)(\s)*$')
        trimmed = space_regex.search(text)
        return trimmed.group(2)

    # Removes character inputted as 2nd argument from ends of string
    else:
        remove_start = re.compile(r'^([%s]+)' % remove)
        remove_end = re.compile(r'([%s]+)$' % remove)
        start = remove_start.search(text)
        end = remove_end.search(text)
        # Allows function to strip even if only one side has remove characters
        try:
            return text[len(start.group()):len(text) - len(end.group())]
        except AttributeError:
            error_avoid = remove + text + remove
            return strip(error_avoid, remove)
def strip ( text , remove = '' ) : # Removes whitespace from either end of the string if remove == '' : space_regex = re . compile ( r'^(\s*)(\S*)(\s)*$' ) trimmed = space_regex . search ( text ) return trimmed . group ( 2 ) # Removes character inputted as 2nd argument from ends of string else : remove_start = re . compile ( r'^([%s]+)' % remove ) remove_end = re . compile ( r'([%s]+)$' % remove ) start = remove_start . search ( text ) end = remove_end . search ( text ) # Allows function to strip even if only one side has remove characters try : return text [ len ( start . group ( ) ) : len ( text ) - len ( end . group ( ) ) ] except AttributeError : error_avoid = remove + text + remove return strip ( error_avoid , remove )