java boilerplate

Thursday, December 01, 2005

compile regular expressions at compile-time.

We propose a new string-like atomic parser item which produces java.util.regex.Pattern objects. The compiler will actually compile these static regular expressions directly. A few APIs should also be extended to take Patterns as valid arguments, such as String.replaceAll and String.matches.

old code:
     //This would throw a runtime exception.
Pattern p = Pattern.compile("*");

new code:
     //This will produce a compile-time error.
Pattern p = R"*";

//This is fine
Pattern k = R"(foo|bar)?\"(.*)\"\\s+";

//This uses the proposed string literal notation.
Pattern r = R"""(foo|bar)?"(.*)"\s+""";


Post a Comment

<< Home