Pattern pets = Pattern.compile( "ant|bat|cat|dog" );
Correct.
Recall (from the previous chapter) that if you want a single backslash in a Java String literal, you need to use two blackslashes. So the Java code
String reg = "\\d+\\w*" ;
results in a string that contains the characters \d+\w*
When you design a regular expression, do so without thinking about Java strings.
Once you have the correct regular expression,
double the backslashes as needed for the argument for compile
.
We want a Pattern
that matches strings that start and
end with optional whitespace characters, and have at least one word character in the middle.
Will the following work?
Pattern pat = Pattern.compile( "\s*\w+\s*" );