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

Question:

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

Asked By: user469652

||

Answers:

you can try strip() if you want to remove front and back, or lstrip() if front

>>> s="  string with front spaces and back   "
>>> s.strip()
'string with front spaces and back'
>>> s.lstrip()
'string with front spaces and back   '

for line in open("file"):
    print line.lstrip()

If you really want to use regex

>>> import re
>>> re.sub("^s+","",s) # remove the front
'string with front spaces and back   '
>>> re.sub("s+Z","",s)
'  string with front spaces and back'  #remove the back
Answered By: ghostdog74

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("", "an bn c") # "anbnc"

# 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+", "", "an bn c", flags = re.MULTILINE)

# but mind that s includes newlines:
r.sub("", "annnn bn c") # "anbnc"

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

re.sub(r"(?m)^s+", "", "an bn c")

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

content = 'an bnn c'
stripped_content = ''.join(line.lstrip(' t') for line in content.splitlines(True))
# stripped_content == 'anbnnc'
Answered By: AndiDog
nowhite = ''.join(mytext.split())

NO whitespace will remain like you asked (everything is put as one word). More useful usualy is to join everything with ' ' or 'n' to keep words separately.

Answered By: Tony Veijalainen

You’ll have to use the re.MULTILINE option:

re.sub("(?m)^s+", "", text)

The “(?m)” part enables multiline.

Answered By: tzot

@AndiDog acknowledges in his (currently accepted) answer that it munches consecutive newlines.

Here’s how to fix that deficiency, which is caused by the fact that n is BOTH whitespace and a line separator. What we need to do is make an re class that includes only whitespace characters other than newline.

We want whitespace and not newline, which can’t be expressed directly in an re class. Let’s rewrite that as not not (whitespace and not newline) i.e. not(not whitespace or not not newline (thanks, Augustus) i.e. not(not whitespace or newline) i.e. [^Sn] in re notation.

So:

>>> re.sub(r"(?m)^[^Sn]+", "", "  ann   nn bn cnd  e")
'annnnbncnd  e'
Answered By: John Machin

You don’t actually need regular expressions for this most of the time. If you are only looking to remove common indentation across multiple lines, try the textwrap module:

>>> import textwrap
>>> messy_text = " grrrn whitespacen everywhere"
>>> print textwrap.dedent(messy_text)
grrr
whitespace
everywhere

Note that if the indentation is irregular, this will maintained:

>>> very_messy_text = " grrrn twhitespacen everywhere"
>>> print textwrap.dedent(very_messy_text)
grrr
        whitespace
everywhere
Answered By: Tim McNamara
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.