在 Java 中匹配非空白 [英] Matching non-whitespace in Java
本文介绍了在 Java 中匹配非空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检测包含非空白字符的字符串.现在我正在尝试:
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?
试试
Pattern p = Pattern.compile( "\\S" );
Matcher m = p.matcher( city );
if( m.find() )
//contains non whitespace
find
方法将搜索部分匹配,而不是完全匹配.这似乎是您需要的行为.
The find
method will search for partial matches, versus a complete match. This seems to be the behavior you need.
这篇关于在 Java 中匹配非空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文