日期:2014-05-20  浏览次数:20693 次

菜鸟请教一则正则表达式
String   str= "asd       asd ";
Pattern   patt1=Pattern.compile( "^\\s$ ");
Matcher   matcher=patt1.matcher(str);
boolean   flag=matcher.find();
System.out.println(flag);
我的意思是验证str中的空格   可是为什么打印出的结果是false?
请教解法,验证字符串中是否有空格  
PS:字符串可以是数字和字母

------解决方案--------------------
^和$表示开始和结束啊,去掉就ok了
------解决方案--------------------
楼上正解
------解决方案--------------------
public class Test {


//测试使用
public static void main(String[] args) {
String str= "asd asd ";
Pattern patt1=Pattern.compile( "\\s+ ");
Matcher matcher=patt1.matcher(str);
boolean flag=matcher.find();
System.out.println(flag);



}

}
------解决方案--------------------
Pattern patt1=Pattern.compile( "^.+\\s.+$ ");

那还不如不用:

Pattern patt1=Pattern.compile( "^.+\\s.+$ ");