【问题标题】:Using a regular expression to replace characters [duplicate]使用正则表达式替换字符[重复]
【发布时间】:2018-02-10 02:45:07
【问题描述】:

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

【问题讨论】:

标签: python regex


【解决方案1】:

使用内置的re 模块。

re.sub(r'[^a-zA-Z ]', '', my_string).lower()

这是一个例子:

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

【讨论】:

  • 应该是r'[^a-z A-Z]'你的将删除OP想要保留的空格
  • @officialaimm:你是对的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
相关资源
最近更新 更多