Reputation: 6244
I'm trying to create a regexp to ignore Freemarker FTL tags in a String.
I've a template with text and FTL tags for example "Hi, [#if gender="Male"]Mr[#elseif]Mrs[/#if] ${name} I've a special offer for you."
I want replace only FTL tags, so only tags that have [#something]
pattern: I'm usings square bracket syntax.
Ideally I should call "myString".replaceAll("regex","");
but I'm not able to find the right regexp to use.
The final String result I want obtain after the replacement is "Hi, Mr Mrs ${name} I've a special offer for you."
Upvotes: 0
Views: 981
Reputation: 4392
The following pattern should do what you expect:
\[/?#(.+?)\]
Test case:
public class Test {
private static final String PATTERN = "\\[/?#(.+?)\\]";
private static final String TEXT = "Hi, [#if gender=\"Male\"]Mr[#elseif]Mrs[/#if] ${name} I've a special offer for you.";
public static void main(String[] args) {
String out = TEXT.replaceAll(PATTERN, "");
System.out.println(out);
}
}
Output:
Hi, MrMrs ${name} I've a special offer for you.
Upvotes: 2
Reputation: 9635
This should do it:
public static void main(String[] args) {
String template = "<html>[BR]\n"
+ "<head>[BR]\n"
+ " <title>Welcome!</title>[BR]\n"
+ "</head>[BR]\n"
+ "<body>[BR]\n"
+ " [#-- Greet the user with his/her name --][BR]\n"
+ " <h1>Welcome ${user}!</h1>[BR]\n"
+ " <p>We have these animals:[BR]\n"
+ " <ul>[BR]\n"
+ " [#list animals as animal][BR]\n"
+ " <li>${animal.name} for ${animal.price} Euros[BR]\n"
+ " [/#list][BR]\n"
+ " </ul>[BR]\n"
+ "</body>[BR]\n"
+ "</html>";
System.out.println("************ORIGINAL TEMPLATE**************");
System.out.println(template);
System.out.println("************REPLACED TEMPLATE**************");
System.out.println(template.replaceAll("\\[/?#(.*?)\\]", "\u001B[33mREPLACEMENT\u001B[0m"));
}
Upvotes: 1