geo*_*ley 6 java regex string whitespace
我想检测其中包含非空白字符的字符串.现在我正在尝试:
!Pattern.matches("\\*\\S\\*", city)
Run Code Online (Sandbox Code Playgroud)
但它似乎没有奏效.有没有人有什么建议?我知道我可以修剪字符串并测试它是否等于空字符串,但我宁愿这样做
你认为正则表达式到底匹配到底是什么?
尝试
Pattern p = Pattern.compile( "\\S" );
Matcher m = p.matcher( city );
if( m.find() )
//contains non whitespace
Run Code Online (Sandbox Code Playgroud)
该find方法将搜索部分匹配,而不是完全匹配.这似乎是你需要的行为.
city.matches(".*\\S.*")
Run Code Online (Sandbox Code Playgroud)
或者
Pattern nonWhitespace = Pattern.compile(".*\\S.*")
nonWhitspace.matches(city)
Run Code Online (Sandbox Code Playgroud)