How to matches anything except space and new line?

Each Answer to this Q is separated by one/two green lines.

I have a string, I just want to match string for any character except for space and new line. What must be regular expression for this?

I know regular expressions for anything but space i.e. [^ ]+ and regular expression for anything but new line [^\n]+ (I’m on Windows). I am not able to figure how to club them together.

You can add the space character to your character class to be excluded.

^[^\n ]*$

Regular expression

^              # the beginning of the string
 [^\n ]*       # any character except: '\n' (newline), ' ' (0 or more times)
$              # before an optional \n, and the end of the string

If you want to exclude just space and newline characters, then you might want to use

r'^[^ \n]*$'

For example,

print re.match(r'^[^ \n]*$', """WelcometoStackoverflow""")
# <_sre.SRE_Match object at 0x7f77a2a58238>
print re.match(r'^[^ \n]*$', """Welcome toStackoverflow""")
# None
print re.match(r'^[^ \n]*$', """Welcome
toStackoverflow""")
# None

Note that it will not eliminate all the other whitespace characters, like tabs, line feed characters etc

print re.match(r'^[^ \n]*$', """Welcome\ttoStackoverflow""")
# <_sre.SRE_Match object at 0x7f77a2a58238>

So if you want to exclude all the whitespace characters then you can use

r'^[^\s]*$'

Or

r'^\S*$'

For example,

print re.match(r'^[^\s]*$', """WelcometoStackoverflow""")
# <_sre.SRE_Match object at 0x7f9146c8b238>
print re.match(r'^[^\s]*$', """Welcome toStackoverflow""")
# None
print re.match(r'^[^\s]*$', """Welcome
toStackoverflow""")
# None
print re.match(r'^[^\s]*$', """Welcome\ttoStackoverflow""")
# None

\S is the same as [^\s]. Quoting from the docs,

\s

When the UNICODE flag is not specified, it matches any whitespace character, this is equivalent to the set [ \t\n\r\f\v]. The LOCALE flag has no extra effect on matching of the space. If UNICODE is set, this will match the characters [ \t\n\r\f\v] plus whatever is classified as space in the Unicode character properties database.

\S

When the UNICODE flags is not specified, matches any non-whitespace character; this is equivalent to the set [^ \t\n\r\f\v] The LOCALE flag has no extra effect on non-whitespace match. If UNICODE is set, then any character not marked as space in the Unicode character properties database is matched.

Try this

[^\s]+

\s is shorthand for whitespaces i.e space( ), newline (\n), tab (\t).


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .