Reputation: 5166
I am trying to create a pattern matcher for anything other than space, that is it should return true if I find anything other than space character, in the entry. I have tried using
String s = "[^ \s]"
as the pattern matcher but it complains as error. What is the correct string to use to generate the pattern matcher?
Upvotes: 2
Views: 1549
Reputation: 785196
it should return true if I find anything other than space character, in the entry.
You don't need any regex for this. You can use:
if (!string.replace(" ", "").isEmpty()) {...}
This will return true if there is any non-space character in the string.
Upvotes: 0
Reputation: 21961
Use capital S
.
String regex= "\\S"
Note: Your code complain error because you should use double \\
inside String.
Upvotes: 2
Reputation: 425043
Look for at least one non-space anywhere in the input:
.*\S.*
In java:
if (input.matches(".*\\S.*"))
// there is at least one non-space character
Or just negate the (simpler) test for all spaces:
if (!input.matches("\\s*"))
Upvotes: 1