go to previous page   go to home page   go to next page

Answer:

Yes.


String Matching

The matches() method matches the string object to a regular expression. The regular expression itself is a string, and must follow correct Java syntax. In particular, the regular expression string must be surrounded by quote marks " " .

String RE = "Daisy|Daisies";

if ( test.matches( RE ) )
    . . .

Bug Alert! The quote marks in "Daisy|Daisies" are not part of the regular expression. If there are space characters (or other white space) inside the quotes, they will be part of the regular expression.


QUESTION 4:

What is the output of the following code fragment?

String test = "Daisy";
   
String RE = "Daisy | Daisies";

if ( test.matches( RE ) )
  System.out.println( "Matches" );
else
  System.out.println( "Does not match" );