"""A Regex version of the Strip Function."""
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)
# Removes character inputted as 2nd argument from ends of string
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
return text[len(start.group()):len(text) - len(end.group())]
error_avoid = remove + text + remove
return strip(error_avoid, remove)
# Get function arguments from user then print stripped string
user_text = input('Enter the text you would like stripped here: ')
user_remove = input('Enter the character you want stripped here'
' (Removes Space as Default): ')
Thanks, I also didn’t realize that I didn’t define ‘pattern’. I also can’t see very well right now. I just went to the eye doctor a couple of days ago and my prescription has gone up in strength by 5! It has been 4 years since I went to see the eye doctor.