这很简单,但我使用Regex相对较新。我想更改以下字符串:
“我爱猫”、“我爱狗”、“我爱猫”、“我爱狗”
我只想知道在任何类型的模式之前删除空格的设置。在这种情况下,大写字母。
发布于 2020-02-05 19:38:44
可以使用与re.sub()相结合的前瞻性断言。
import re
s = ' I love cats'
re.sub(r'''^ # match beginning of string
\s+ # match one or more instances of whitespace
(?=[A-Z]) # positive lookahead assertion of an uppercase character
''','',s,flags=re.VERBOSE)并向您展示在小写字母之前没有删除空格:
s = ' this is a test'
re.sub(r'^\s+(?=[A-Z])','',s)结果:
' this is a test'https://stackoverflow.com/questions/60082855
复制相似问题