python - Why is re.sub is not replacing string with empty string -
i can't understand pythons re.sub behavior. want replace entire multi line string nothing. want empty string back:
#!/usr/bin/python import re if __name__ == "__main__": data = """hello world day """ textarea = re.sub( '.*', '', data) print "processed '%s'" % textarea textarea = re.sub( '.*', '', data, flags=re.multiline) print "processed '%s'" % textarea
the above code, @ least on machine not output following:
processed ''
this both cases, multiline/non-multiline. instead single quotes spread across multiple lines. why so?
what want delete empty lines ( lines containing 0 or more spaces) multiline string, , believe above example issue.
thanks.
answer:
as others pointed out. multiline confusing me. answer original question, instead of desire, dot not matching newline, hence getting replaced empty string, except newlines in original string left alone.
by adding re.dotall flag above example gave expected behavior of empty string.
what want delete empty lines ( lines containing 0 or more spaces) multiline string.
re.sub(r'(?m)^[ \t]*$\n?', '', s)
or
re.sub(r'(?m)^\s*$\n?', '', s)
Comments
Post a Comment