String test = "\"This is a backslash \\\", she said."; System.out.print( test );
"This is a backslash \", she said.
Regular Expression | Matches |
---|---|
\[dog\] | [dog] |
\\cat | \cat |
3\.14159 | 3.14159 |
A\+ | A+ |
Recall (from Chapter 7) that you need a backslash in a regular expression to remove the meaning of a metacharacter.
When using the matches(String RE)
method,
the argument RE must refer to a Java String
.
So sometimes you need a String
that contains backslashes
that are part of the RE.
To do this, use two backslashes in the String
literal for each
backslash you want in the regular expression.
So, if the RE you want to end up with is:
3\.14159
Then the String
literal should be:
"3\\.14159"
Say that you want the regular expression \[dog\]
which literaly matches the seven characers: \[dog\]
Write the code that puts those characters into a String
: