[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6032
  • Last Modified:

Urgent: Need to escape curly braces in property file.

Prolly really easy, but it's urgent, hence the 500 points
I'm storing regular expressions in property files based on language/country pairs. I'm using the struts internationalisation and message-bundles, reading my property-files and exporting it into a javascript for use of validation.
I need to know how to escape the curly braces in such a way that when I use <bean:message ... /> to output it, the curly braces are used as output. A simple example:

Should output to: ^(\w\s?){2,25}$

Right now struts won't be able to output this because it expects arguments in the {0} style and finds unexpected characters inbetween the curly braces...
  • 4
  • 4
1 Solution
Shouldn't it be:

inputpattern = "^(\\w\\s?){2,25}$" ;

?  I'm confused...  can you give a fuller example?
mreuringAuthor Commented:
My, current, inbetween workaround:

<bean:message arg0="{" arg1="}" key="inputpattern"/>

But I'm pretty damn sure there's an easier way to go about getting curly braces printed from a properties file...
I'd be surprised if it was anything more complicated than:
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

mreuringAuthor Commented:
In struts you can define a message bundle and use the associated .properties files in combination with the bean taglib to print internationalised messages. Since I'm building an internationalised form that has different validation rules per country/language I'm storing regular expressions in the property files and print them out in my internationalised javascript file (based on the current locale of the user).
Basic rules for a properties file:

And backslash being the escape character.
Struts adds to this that in the value of a property you can define arguments:

Where n is an integer value for the argument. Problem is that a regular expression also uses curly braces and I now have to find a way to make struts ignore the curly braces entered into the property pair. And the escape char isn't enough, I tried:

Neither solution succesfully escapes the curly brace for use with the bean taglib.
But, I am surprised...

From the MessageFormat docs:
Thus, a string that should result in the formatted message "'{0}'" can be written as "'''{'0}''" or "'''{0}'''".

So, you want:
mreuringAuthor Commented:
Thanx for the quick suggestions!

I was getting my hopes up, but this only changes the error message:
java.lang.IllegalArgumentException: can't parse argument number ''1

Although I did combine your earlier suggestion with my current workaround, making it:

<bean:message arg0="{" key="inputpattern"/>

Still not verry elegant :(

Sadly, you seem to have found the best option IF you have to use the Struts tags.  Maybe you should consider a custom tag, or some other way of retrieving your regular expressions.

The problem is due to org.apache.struts.util.MessageResources.   The property values are passed to the MessageFormat constructor, so you (I) would expect to be able to quote the curlies by surrounding them in quotes.  However, just before it calls the MessageFormat constructor it preprocesses the property value and doubles up all the quotes:

    public String getMessage(Locale locale, String key, Object args[]) {
            format = new MessageFormat(escape(formatString));

     * Escape any single quote characters that are included in the specified
     * message string.
     * @param string The string to be escaped
    protected String escape(String string) {

        if ((string == null) || (string.indexOf('\'') < 0))
            return (string);
        int n = string.length();
        StringBuffer sb = new StringBuffer(n);
        for (int i = 0; i < n; i++) {
            char ch = string.charAt(i);
            if (ch == '\'')
        return (sb.toString());


So, basically, you cannot escape the {} processing, and what you have done is about as good as it gets...

mreuringAuthor Commented:
What the hell were they thinking?
Well, on basis of 'enlightening beyond the direct question' I most certainly find this worth a grade A :)

I've found an entry in the struts/apache bug database, so you can vote for it or give feedback etc if you wish:

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now