I would like to detect strings that have non-whitespace characters in them. Right now I am trying:
!Pattern.matches("\\*\\S\\*", city)
But it doesn't seem to be working. Does anyone have any suggestions? I know I could trim the string and test to see if it equals the empty string, but I would rather do it this way
What exactly do you think that regex matches?
Try
Pattern p = Pattern.compile( "\\S" );
Matcher m = p.matcher( city );
if( m.find() )
//contains non whitespace
The find method will search for partial matches, versus a complete match. This seems to be the behavior you need.
city.matches(".*\\S.*")
or
Pattern nonWhitespace = Pattern.compile(".*\\S.*")
nonWhitspace.matches(city)
                        \S (uppercase s) matches non-whitespace, so you don't have to negate the result of matches.
Also, try Matcher's method find instead of Pattern.matches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With