Python 使用正则表达式替换字符

Python 使用正则表达式替换字符,python,regex,Python,Regex,我想删除标题中除a-z、a-z和空格以外的所有字符。然后我想将所有字符转换为小写。使用内置模块 下面是一个例子: >>> re.sub(r'[^a-zA-Z ]', '', 'This is 1 test string; I like it.').lower() 'this is test string i like it' 欢迎来到堆栈溢出!下次,请查看我们的页面。如果regex不是mandotory,请使用良好的.lower,因此问题()应该是r'[^a-z a-z]。

我想删除标题中除
a-z
a-z
和空格以外的所有字符。然后我想将所有字符转换为小写。

使用内置模块

下面是一个例子:

>>> re.sub(r'[^a-zA-Z ]', '', 'This is 1 test string; I like it.').lower()
'this is  test string i like it'

欢迎来到堆栈溢出!下次,请查看我们的页面。如果regex不是mandotory,请使用良好的
.lower
,因此问题()应该是
r'[^a-z a-z]。
您的问题也将删除OP想要删除的空格keep@officialaimm:你说得对。
>>> re.sub(r'[^a-zA-Z ]', '', 'This is 1 test string; I like it.').lower()
'this is  test string i like it'