If you want to exclude only space and newlines, then you can use
r'^[^ \n]*$'
For example,
print re.match(r'^[^ \n]*$', """WelcometoStackoverflow""")
Please note that it will not eliminate all other space characters, such as tabs, line characters, etc.
print re.match(r'^[^ \n]*$', """Welcome\ttoStackoverflow""")
So, if you want to exclude all whitespace, you can use
r'^[^\s]*$'
or
r'^\S*$'
For example,
print re.match(r'^[^\s]*$', """WelcometoStackoverflow""")
\S matches [^\s] . Quoting from documents,
\ s
If the UNICODE flag is not specified, it matches any space character, this is equivalent to the set [ \t\n\r\f\v] . The LOCALE flag does not have an additional effect on space mapping. If UNICODE set, this will match the characters [ \t\n\r\f\v] plus everything that is classified as a space in the Unicode character property database.
\ S
If UNICODE flags are not specified, they match any character without spaces; this is equivalent to the set [^ \t\n\r\f\v] The LOCALE flag does not have an additional effect on matching without spaces. If UNICODE is specified, then any character that is not marked as a space in the Unicode character property database is mapped.
thefourtheye
source share