-1

Desired output : Java 1 8 true Output produced by this code Java 1 8 false what should be done to return true value ? the pattern of the input is [string] [dot][digit][dot][digit].

String st=java 1.8;
String regex="[\\s\\.]";
Pattern p=Pattern.compile(regex);
String []s2=p.split(st);
for(String a:s2)
System.out.println(a);
System.out.println(p.matches(st,regex));

} }

Achudha N
  • 11
  • 2

1 Answers1

1

You are splitting the string - thus it searches for a portion on your string for that regex.

When you use matches (which is static and the arguments are in reverse order), you are matching against the entire string.

So for example:

System.out.println(Pattern.matches(regex, st));

where regex would change to ".+[\\s\\.].+" would print true.

Eugene
  • 117,005
  • 15
  • 201
  • 306