I assume that when you say "all '\r' \t' '\n'...other than spaces", what you mean is "any whitespace character other than U+0020" (where U+0020 is a simple space). Is this correct?
If so, then the following regex (general form) should work:
(?! )\s
This will match any whitespace character that is not a simple space. This regex makes use of negative lookahead.
EDIT:
As @Bubletan states in their answer, the following regex will also work:
[^\S ]
Both of these regex are equivalent. This is because (?! )\s ≣ "(is NOT the character U+0020) AND (is whitespace)"
and [^\S ] ≣ "is NOT (non-whitespace OR the character U+0020)
have the same truth table:
Let P(x) be the predicate "x is the character U+0020"
Let Q(x) be the predicate "x is whitespace"
P | Q | (¬P)∧Q | ¬(¬Q∨P)
–– ––– –––––––– ––––––––
T T F F
T F T T
F T F F
F F F F
Although for the sake of efficiency, you are probably better off using @Bubletan's solution ([^\S ]
). Lookaround is generally slower than the alternative.
This is how you could implement it:
// Create the pattern. (do only once)
Pattern pattern = Pattern.compile("[^\\S ]");
// Test an input string. (do for each input)
Matcher matcher = pattern.matcher(string);
boolean result = matcher.find();
result
will then indicate whether string
contains any whitespace other than a simple space.