^(\s+) only removes the whitespace from frist line, how to remove the front whitespace from all the lines?
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
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"
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.
You'll have to use the re.MULTILINE option:
re.sub("(?m)^\s+", "", text)
The "(?m)" part enables multiline.
@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. [^\S\n]
in re
notation.
So:
>>> re.sub(r"(?m)^[^\S\n]+", "", " a\n\n \n\n b\n c\nd e")
'a\n\n\n\nb\nc\nd e'