Remove special symbols from String

89 views Asked by At

I'm working with Stardog in this Java code:

Connection aConn = ConnectionConfiguration
            .to("")
            .server("http://localhost:5820")
            .database("TP_OntologiasEjecutado")
            .credentials("admin", "admin")
            .connect()
            .as(Connection.class);

SelectQuery selectQuery = aConn.select(
    "SELECT DISTINCT ?Libreta ?Puntaje \n"+
    "WHERE{ \n"+
      "?Alumno a :PostulanteABecaAdmisible. \n"+
      "?Alumno :cantidadMateriasAprobadasCicloLectivoAnterior ?matAnterior. \n"+
      "?Alumno :promedioAlumno ?Promedio. \n"+
      "?Alumno :numeroLegajo ?Libreta. \n"+
      "?Alumno :medicionFinal ?Puntaje. \n"+
      "?Alumno :seInscribeAConvocatoria ?conv. \n"+
      "?conv :anioConvocatoria ?anioConv. \n"+
      "FILTER (?anioConv = "+anio+"). \n"+
      "FILTER (?Promedio >= "+promedio+"). \n"+
      "FILTER (?matAnterior >= "+materias+"). \n"+
    "} \n"+
    "ORDER BY DESC (?Puntaje)"
    );

ByteArrayOutputStream stream = new ByteArrayOutputStream();
    SelectQueryResult selectQueryResult = selectQuery.execute();

    try{
        QueryResultWriters.write(selectQueryResult, stream, HTMLQueryResultWriter.FORMAT);
    } catch (IOException e) {
        System.out.println("ERROR");
    }

This generates HTML code that I show in a JLabel in a JPanel, this is an example of the code I get: HTML code and the result

And this is the opening the HTML in Notepad++:

<html>
<head><meta content="text/html;charset=UTF-8"/></head>
<body>
    <table border=1>
        <tr>
            <th>Libreta</th>
            <th>Puntaje</th>
        </tr>
        <tr>
            <td style="text-align:left;vertical-align:top">23806</td>
            <td style="text-align:left;vertical-align:top">&quot;67.75&quot;^^&lt;http://www.w3.org/2001/XMLSchema#float&gt;</td>
        </tr>
        <tr>
            <td style="text-align:left;vertical-align:top">123456</td>
            <td style="text-align:left;vertical-align:top">&quot;66.6&quot;^^&lt;http://www.w3.org/2001/XMLSchema#float&gt;</td>
        </tr>
    </table>
</body>
So what I need is to remove the ^^<http://www.w3.org/2001/XMLSchema#float>. I could remove the URL:
String finalString = new String(stream.toByteArray()).replaceAll("http://www.w3.org/2001/XMLSchema#float","");

But I can't remove the ^^<> part because they are special characters. How can I remove them?

1

There are 1 answers

2
Peter Schoener On

The issue is that if you escape those characters in your regex, e.g.

String finalString = new String(stream.toByteArray()).replaceAll("\^\^\<http://www.w3.org/2001/XMLSchema#float\>","");

you're escaping them in the string that gets created. If this even works without errors, your regex string will still be

"^^<http://www.w3.org/2001/XMLSchema#float>"

So what you need to do is escape your backslashes, turning them into single backslashes underneath, and ensuring that they are still present when needed to escape the special regex characters:

String finalString = new String(stream.toByteArray()).replaceAll("\\^\\^<http://www.w3.org/2001/XMLSchema#float>","");