dumper
dumper

Reputation: 485

How to remove \r\n character in regex python

formatter_fn = lambda sentence: re.sub(r'([^\s\w\.])+', '', sentence).lower()
formatter_fn('\r\ndirected; by Nolan.')

This gives the output as \r\ndirected by nolan. but I want it as directed by nolan.

How to remove \r\n in this case ?

Thanks!

Upvotes: 2

Views: 3703

Answers (1)

rock321987
rock321987

Reputation: 11032

Try this

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

\s is equivalent to the set [ \t\n\r\f] but you require only blank space (I guess).

So when you use

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

It will be match anything except ( space, \t, \n, \r, \f ) <-- part of \s, \w, and .. So it was unable to match \r and \n in your string.

If you want to include \t in your set, then you can use

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

Upvotes: 3

Related Questions