黑马程序员技术交流社区

标题: 为什么的写的代码有问题? [打印本页]

作者: 细听风语为梧桐    时间: 2016-9-9 23:49
标题: 为什么的写的代码有问题?
public static void main(String[] args) throws Exception {
     String [] str ={"16210626656","18601066888","13912387666","13156166693","15115888028"};
     String regex ="[1][358][0-9]{6}(\\d){3}";
     Pattern p = Pattern.compile(regex);
     Matcher m = p.matcher(str);
     boolean b = m.matches();
     while(m.find()){System.out.println(m.group());}
        }
-----------------------------------------------------------------------
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
        The method matcher(CharSequence) in the type Pattern is not applicable for the arguments (String[])

        at day14_try.Num2.main
---------------------------------------------------------------------我都无语了,怎么回事呢


作者: xuheng8600    时间: 2016-9-10 14:04
boolean b = m.matches();     //这一句不要,错误的;
正确的代码这样写,你试试:
String str ="我的手机是18511866260,我曾用过18987654321,还用过18812345678";  //不用String[ ]看看
     String regex ="[1][358][0-9]{6}(\\d){3}";
     Pattern p = Pattern.compile(regex);
     Matcher m = p.matcher(str);
     boolean b = m.matches();
     while(m.find()){
            System.out.println(m.group());}
     }




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2