如何在regex python中删除\ r \ n字符

时间:2016-04-03 06:36:27

标签: python regex

let store = {
  data: Immutable.Map({ activeKey: 1 })
};

这会将输出设为formatter_fn = lambda sentence: re.sub(r'([^\s\w\.])+', '', sentence).lower() formatter_fn('\r\ndirected; by Nolan.') ,但我希望它为\r\ndirected by nolan.

在这种情况下如何删除directed by nolan.

谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个

re.sub(r'[^ \w\.]', '', sentence).lower()

\s相当于集合[ \t\n\r\f],但您只需要空格(我猜)。

所以当你使用

re.sub(r'[^\s\w\.]', '', sentence).lower()

除了space\t\n\r\f)< - {{1}的一部分外,它将匹配任何内容},\s\w。因此无法在字符串中匹配.\r

如果您想在集合中加入\n,则可以使用

\t