Illegal group Reference error

Asked

Viewed 402 times

1

You’re making a mistake on a replace what I’m doing. The string in question is this:

<table id='tablecritica' class='table table-bordered table-hover'>      <tbody>  <tr>   <td> Valor Contábil (R$) </td>      <td style='text-align: right;'> 1.076,63 </td>  </tr> <tr class='fundo total table-secondary'>      <td> Total A (R$) </td>     <td> 1.076,63 </td>  </tr> <tr style='font-weight: bold'>   <td> Comparativo </td>      <td style='text-align: right;'> <> </td>  </tr> <tr>    <td> Base de Cálculo (R$) </td>     <td style='text-align: right;'> 0,00 </td>  </tr> <tr>      <td> Isenta Não Tributada (R$) </td>    <td style='text-align: right;'> 0,00 </td>  </tr> <tr>      <td> Outras (R$) </td>      <td style='text-align: right;'> 1.076,63 </td>  </tr> <tr>      <td> Imposto Retido ST (R$) </td>   <td style='text-align: right;'> 0,00 </td>  </tr> <tr>      <td> Imposto Retido Subs. ST (R$) </td>     <td style='text-align: right;'> 0,00 </td>  </tr> <tr>      <td> Outros Impostos (R$) </td>     <td style='text-align: right;'> 0,00 </td>  </tr> <tr class='fundo total table-secondary'>      <td> Total B (R$) </td>     <td> 1.076,63 </td>  </tr>   </tbody>     </table>

I am sending this script as a parameter to an HTML template, where I read the HTML and only replicate the parameters.

Hashtable valuesReplace = new Hashtable();
valuesReplace.put("Tabelacritica",  gb.nullToEmpty(  tabelaCritica ));

Hashtable hashReplace = (Hashtable) valuesReplace;

String templateEmail = gb.getPropertyAplication("Path") + "/" + templateName;

        StringBuilder stringBuilder = new StringBuilder();

        java.io.File file = new java.io.File(templateEmail);  

        java.io.FileInputStream fileInputStream = new java.io.FileInputStream( file );
        java.io.BufferedReader bufferedReader = new java.io.BufferedReader( new java.io.InputStreamReader(fileInputStream) );

        while ( ( row = bufferedReader.readLine() ) != null ) {

            row = new String(row.trim().getBytes(), "UTF-8");    
            stringBuilder.append( row );

        }             

        String contentEmail = stringBuilder.toString();        
        String str;
        java.util.Enumeration keys = hashReplace.keys();

        while(keys.hasMoreElements()) {

            str = (String) keys.nextElement();

            contentEmail = contentEmail.replaceAll("\\{" + str + "\\}", hashReplace.get(str).toString());   

        }

If I send any other parameter as string works normally.

  • Some tips not directly related to replace: Hashtable is obsolete, prefer to use HashMap (or any other implementation of Map). Next time, try to keep the example more brief, only with the necessary (for example, your code has read from a file that we don’t know what it is, and doesn’t seem relevant to the problem) - try to make a [mcve] read this link, has many tips to make an example that does not depend on external resources (such as files) and helps a lot who answer...

  • ... in this case it was possible to know the cause of the problem, but it is not always so (if the problem was in the contents of the file, for example, no one could simulate the error). That’s why it’s important to try to make a [mcve]

1 answer

2

The problem is that your string contains the character $ (in various parts, such as "Total A (R$)", among others).

And in the second parameter of method replaceAll the character $ has special meaning: it serves to designate the match obtained in a catch group: the syntax is $N, where N is the number of the respective group (ie, $1, $2, etc.).

But in your case the $ is not referring to any group, but to the character itself $, without any special meaning. Therefore, you should make the escape from it, using the method Matcher.quoteReplacement:

contentEmail = contentEmail.replaceAll("\\{" + str + "\\}", Matcher.quoteReplacement(hashReplace.get(str).toString()));

In the end, what quoteReplacement does is put a \ before the $, for thus it comes to be interpreted as a common character, with no special meaning. But as your strings seem to be large, I don’t think it’s a good idea to do this substitution manually. Up because the documentation of replaceAll mentions that the character \ also suffers from this same problem and needs to be escaped, so use quoteReplacement that already takes care of those details for you (if the string doesn’t have any $ or \, for example, it is returned without modifications, so you can use for all your strings).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.