Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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语句模式匹配_Java_Regex - Fatal编程技术网

Java语句模式匹配

Java语句模式匹配,java,regex,Java,Regex,我试图编写一个简单的模板引擎来解析if语句 <{if $value == true}> say hello <{/if}> 代码解析并返回: say hello <{/if}> ... <{if $value2 == true}> say hello 这可能是因为贪婪的天性 试试这个 (\\p{ASCII}+?)而不是(\\p{ASCII}+)我明白了,谢谢这是一个古老的页面(1.4.x)。以下是当前版本:请注意,如果有

我试图编写一个简单的模板引擎来解析
if
语句

<{if $value == true}>
    say hello
<{/if}>
代码解析并返回:

    say hello
<{/if}>
...
<{if $value2 == true}>
    say hello

这可能是因为贪婪的天性

试试这个


(\\p{ASCII}+?)
而不是
(\\p{ASCII}+)

我明白了,谢谢这是一个古老的页面(1.4.x)。以下是当前版本:请注意,如果有嵌套的if语句,则不能使用正则表达式来解析模板。
    say hello
<{/if}>
...
<{if $value2 == true}>
    say hello
    public class Templates {

        private static String escape(String value) {
            return value.replaceAll("\\$", "\\\\\\$");
        }

        public static String load(String name) {
            return load(name, null);
        }

        public static String load(String name, Map<String, String> parse) {

            String page = new Resources().getTextResource("lib/tpl/" + name);

            if (page == null) {
                return "The template, " + name + " was NOT FOUND.";
            }

            if (parse != null) {
                Iterator it = parse.entrySet().iterator();

                while (it.hasNext()) {

                    Map.Entry entry = (Map.Entry) it.next();
                    String key = (String) entry.getKey();
                    String value = (String) entry.getValue();

                    value = escape(value); // Prevents java exception. Can't deal with $

                    page = page.replaceAll("\\<\\{\\$" + key + "\\}\\>", value);


                }

                Pattern ptrn = Pattern.compile("\\<\\{if \\$([a-z]+)\\s*(==|!=|eq|neq|or|and)\\s*(\\w+)\\}\\>(\\p{ASCII}+)\\<\\{/if\\}\\>");

                Matcher mtch = ptrn.matcher(page);

                System.out.println("\n\n\n");

                while(mtch.find()) {

                    System.out.println("Key is: " + mtch.group(1));
                    //System.out.println("Key: " + dateMatcher.group(2));
                    System.out.println("Value is: " + mtch.group(3));
                    System.out.println("Data is: " + mtch.group(4));

                    if(parse.get(mtch.group(1)).equals(mtch.group(3))) {
                        System.out.println("\n\n\nREPLACE");
                        page = page.replaceAll(ptrn.pattern(), escape(mtch.group(4)));
                    } else {
                        //dateMatcher.appendReplacement(page, "");
                        System.out.println("\n\n\nREMOVE - " + ptrn.pattern());
                        page = page.replaceAll(ptrn.pattern(), "");
                    }

                }

                System.out.println("\n\n\n");


            }
            return page;


}
}