automate-boring-stuff/exercises/ex07/ex07d.py

26 lines
467 B
Python

"""
Project: Regex Version of the strip() Method
Page 186
"""
import re
tests = {
"Hola": None,
" Hola ": None,
"bHola": "b",
"\tHola,\tmundo\t": "\t",
}
def rgx_strip(string, char=" "):
string = re.sub(r"^" + char + r"+", "", string)
return re.sub(char + r"+$", "", string)
for key, val in tests.items():
if val is None:
print(f"'{key}' -> '{rgx_strip(key)}'")
else:
print(f"'{key}' -> '{rgx_strip(key, val)}'")