Java Reference
In-Depth Information
System.out.println(result);
// This will test to see if any numbers are present, in
this case the
// person writing this String would be able to like any
Java release!
str = "I love Java 8!";
result = str.matches("I love Java [0-9]!");
System.out.println(result);
// This will test TRUE as well...
str = "I love Java 7!";
result = str.matches("I love Java [0-9]!");
System.out.println(result);
// The following will test TRUE for any language that
contains
// only one word for a name. This is because it tests for
// any alphanumeric combination. Notice the space
character
// between the numeric sequence...
result = str.matches("I love .*[ 0-9]!");
System.out.println(result);
// The following String also matches.
str = "I love Jython 2.5.4!";
result = str.matches("I love .*[ 0-9]!");
System.out.println(result);
Each of the results printed out in the example will be true , with the exception of
the second example because it does not match.
Solution #2
Use the regular expression Pattern and Matcher classes for a better-performing
and more versatile matching solution than the string matches() method. Although
the matches() method will get the job done most of the time, there are some occa-
Search WWH ::




Custom Search