Java正则表达不匹配

时间:2013-05-09 05:52:21

标签: java regex

Pattern pattern = Pattern.compile("v(\\d\\.\\d)");
String s = "<a href>asldkjaskldj<>kljlaksjd v1.3<asdas>";
System.out.println(pattern.matcher(s).group());

为什么这种模式与1.3不匹配?

2 个答案:

答案 0 :(得分:2)

这对我有用:

    Pattern pattern = Pattern.compile("v(\\d\\.\\d)");
    String s = "<a href>asldkjaskldj<>kljlaksjd v1.3<asdas>";
    Matcher matcher = pattern.matcher(s);
    matcher.find();
    System.out.println(matcher.group());

匹配v1.3。

在您尝试find之前,group不会向您提供结果

答案 1 :(得分:1)

我试过这个,它对我有用。

        String s = "<a href>asldkjaskldj<>kljlaksjd v1.3<asdas>";
    Pattern pattern = Pattern.compile("v(\\d\\.\\d)");
    Matcher m = pattern.matcher(s);
    if(m.find()){
        System.out.println("matches");
    }else{
        System.out.println("no match");
    }