Chapter 7, Automate the boring stuff with Python, practice project: regex version of strip()
- 阅读更多 关于 Chapter 7, Automate the boring stuff with Python, practice project: regex version of strip()
问题 I am reading the book "Automate the boring stuff with Python'. In Chapter 7, in the project practice: the regex version of strip(), here is my code (I use Python 3.x): def stripRegex(x,string): import re if x == '': spaceLeft = re.compile(r'^\s+') stringLeft = spaceLeft.sub('',string) spaceRight = re.compile(r'\s+$') stringRight = spaceRight.sub('',string) stringBoth = spaceRight.sub('',stringLeft) print(stringLeft) print(stringRight) else: charLeft = re.compile(r'^(%s)+'%x) stringLeft =