Parsing with MessageFormat: Is there a way to force greedy matching, or to require that the entire input be matched?
<p>Or: is there a another utility capable of both <a href="https://docs.oracle.com/javase/8/docs/api/java/text/Format.html#format-java.lang.Object-" rel="nofollow">formatting</a> strings and <a href="https://docs.oracle.com/javase/8/docs/api/java/text/Format.html#parseObject-java.lang.String-" rel="nofollow">parsing</a> strings (using the same object!) that can achieve this?</p>

<p>Suppose you have a directory full of audio files in <code>.au</code> format with names following the pattern <code>country.code.XX.au</code>.  We might like to format and parse the significant parts of these filenames using a <code>MessageFormat</code> with pattern <code>country.{0}.au</code>.</p>

<p>But consider this demo:</p>

<pre><code>public class MessageFormatExercise {
    public static void main(String[] args) throws Exception {
        java.text.Format f = new java.text.MessageFormat("country.{0}.au");

        // String formatting; prints "country.code.us.au" and "country.code.au.au" respectively
        System.out.println(f.format(new Object[]{"code.us"}));
        System.out.println(f.format(new Object[]{"code.au"}));

        // String parsing; prints "code.us" and "code" respectively (not "code.au"!)
        System.out.println(((Object[]) f.parseObject("country.code.us.au"))[0]);
        System.out.println(((Object[]) f.parseObject("country.code.au.au"))[0]);
    }
}
</code></pre>

<p>The parse results are not consistent.  In some cases we get <code>code.us</code> or <code>code.gb</code> but in the case of Australia we get simply <code>code</code>.</p>