Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: use regular expression to remove the white space from all lines

Tags:

python

regex

^(\s+) only removes the whitespace from the first line. How do I remove the front whitespace from all the lines?

like image 258
user469652 Avatar asked Oct 21 '10 05:10

user469652


People also ask

How do I remove all white spaces from a string in Python?

strip() Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.

How do I get rid of white space in regex?

The replaceAll() method accepts a string and a regular expression replaces the matched characters with the given string. To remove all the white spaces from an input string, invoke the replaceAll() method on it bypassing the above mentioned regular expression and an empty string as inputs.

How do I remove all white spaces from a string?

The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".

How do you stop a space in regex?

You can easily trim unnecessary whitespace from the start and the end of a string or the lines in a text file by doing a regex search-and-replace. Search for ^[ \t]+ and replace with nothing to delete leading whitespace (spaces and tabs). Search for [ \t]+$ to trim trailing whitespace.


1 Answers

Python's regex module does not default to multi-line ^ matching, so you need to specify that flag explicitly.

r = re.compile(r"^\s+", re.MULTILINE) r.sub("", "a\n b\n c") # "a\nb\nc"  # or without compiling (only possible for Python 2.7+ because the flags option # didn't exist in earlier versions of re.sub)  re.sub(r"^\s+", "", "a\n b\n c", flags = re.MULTILINE)  # but mind that \s includes newlines: r.sub("", "a\n\n\n\n b\n c") # "a\nb\nc" 

It's also possible to include the flag inline to the pattern:

re.sub(r"(?m)^\s+", "", "a\n b\n c") 

An easier solution is to avoid regular expressions because the original problem is very simple:

content = 'a\n b\n\n c' stripped_content = ''.join(line.lstrip(' \t') for line in content.splitlines(True)) # stripped_content == 'a\nb\n\nc' 
like image 76
AndiDog Avatar answered Sep 26 '22 22:09

AndiDog