Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用MessageFormat解析:有没有办法强制贪婪匹配,或者要求匹配整个输入?_Java_String Formatting_String Parsing_Greedy_Messageformat - Fatal编程技术网

Java 使用MessageFormat解析:有没有办法强制贪婪匹配,或者要求匹配整个输入?

Java 使用MessageFormat解析:有没有办法强制贪婪匹配,或者要求匹配整个输入?,java,string-formatting,string-parsing,greedy,messageformat,Java,String Formatting,String Parsing,Greedy,Messageformat,或者:是否有另一个能够同时使用字符串和字符串(使用同一对象!)的实用程序可以实现这一点 假设您有一个目录,其中满是.au格式的音频文件,其名称遵循模式country.code.XX.au。我们可能希望使用带有模式country.{0}.au的MessageFormat格式化和解析这些文件名的重要部分 但请考虑这个演示: public class MessageFormatExercise { public static void main(String[] args) throws Ex

或者:是否有另一个能够同时使用字符串和字符串(使用同一对象!)的实用程序可以实现这一点

假设您有一个目录,其中满是
.au
格式的音频文件,其名称遵循模式
country.code.XX.au
。我们可能希望使用带有模式
country.{0}.au
MessageFormat
格式化和解析这些文件名的重要部分

但请考虑这个演示:

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.us
code.gb
,但在澳大利亚,我们得到的只是
code

现代化的
com.ibm.icu.text.MessageFormat
,带有命名模板参数和更健全的引用规则,表现出相同的行为,产生
code
而不是
code.au