Search code examples
javaregexsmartygettextpoedit

Regex: Read value between multiple brackets


I currently working on translating a website (Smarty) with Poedit. To get all the text from the .tpl files i'm using regex to get the data between the {t} and {/t}. so an example:

{t}Password incorrect, please try again{/t}

The regex will read Password incorrect, please try again and place it in a .po file. This is all working fine. It goes wrong when it gets a little more advanced.

Sometimes the text between the {t} tags uses a parameter. this looks like this:

{t 1=$email|escape 2=$mailbox}No $1 given, please check your $2{/t}

This is also working great.

The real problem start when i use brackets inside the parameter like this:

{t 1={site info='name'} 2=$mailbox}visit %1 or go to your %2{/t}

My regex will close when it sees the first closing brackets so the result will be 2=$mailbox}visit %1 or go to your %2.

My regex looks like this:

\{t.*?\}?[}]([^\{]+)\{\/t\}|\{t\}([^\{]+)\{\/t\}

The regex is used inside a java program.

Does anybody has a way to fix this problem?


Solution

  • The easiest solution I see on this is to normalize the .tpl files. Just use a regex which matches all tags something like this one:

    {[^}]*[^{]*}
    

    I had the same issue to solve and it worked pretty good with the normalizing.

    The normalizing-method would look like this:

    final String regex = "\\{[^\\}]*[^\\{]*\\}";
    
    private String normalizeContent(String content) {
        return content.replaceAll(regex, "");
    }