Thursday, April 22, 2010

[TF002] dot-star matches all strings! (true/false?)

(true/false?) In regex, the dot metacharacter matches everything, and the star allows zero or more repetition, so for all s instanceof String, s.matches(".*").
static void tf002(String s) {
   if (s instanceof String) {
      assert s.matches(".*");

FALSE! From the Pattern API:
The regular expression . matches any character except a line terminator unless the DOTALL flag is specified.
tf002("\n"); // throws AssertionError
The correct statement is that for all s instanceof String, s.matches("(?s).*"). Note that (?s) is the embedded flag expression for Pattern.DOTALL.

Interestingly, Bug# 6565414 requests that .* be optimized since it "is always going to be true". The above counterproof demonstrates that this is incorrect: it's only trivially true in the so-called "single-line" mode.

No comments:

Post a Comment