Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove tabs and newlines with a regex

In Python 3.x, the special re sequence '\s' matches Unicode whitespace characters including [ \t\n\r\f\v].

The following piece of code is intended to replace tabs and newlines with a space.

import re
text = """Hello my friends.
    How are you doing?
I'm fine."""
output = re.sub('\s', ' ', text)
print(output)

However, the tab is still present in output. Why?

like image 915
Marcos Gonzalez Avatar asked May 03 '13 09:05

Marcos Gonzalez


People also ask

What is the regex for newline?

"\n" matches a newline character.

How do I remove a specific character from a regular expression?

If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")

How do I remove a tab from a string in Java?

You can remove any character/string from a string by using the Replace function. This allows you to specify the string you want to replace, and the value you want to replace it with. When you want to "remove" you simple use an empty string as the replace value.


1 Answers

The problem is(likely) that your tab character is just a bunch of spaces.

>>> re.sub(r"\s+", " ", text)
"Hello my friends. How are you doing? I'm fine."
like image 67
Nolen Royalty Avatar answered Sep 30 '22 05:09

Nolen Royalty