如何匹配除空格和换行符以外的任何内容?


问题内容

我有一个字符串,我只想匹配除空格和换行以外的任何字符的字符串。为此必须使用正则表达式吗?

我知道除空格以外的任何内容的[^ ]+正则表达式,以及除换行以外的其他任何内容的正则表达式[^\n]+(我在Windows上)。我无法弄清楚如何将它们组合在一起。


问题答案:

您可以将空格字符添加到要排除的字符类中。

^[^\n ]*$

正则表达式

^              # 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