【问题标题】:Remove special symbols from String [duplicate]从字符串中删除特殊符号[重复]
【发布时间】:2020-11-19 07:53:26
【问题描述】:

我正在这段 Java 代码中使用 Stardog:

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");
    }

这会生成我在 JPanel 中的 JLabel 中显示的 HTML 代码,这是我得到的代码示例: HTML code and the result

这是在 Notepad++ 中打开 HTML:

<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>
所以我需要删除 ^^。我可以删除网址:
String finalString = new String(stream.toByteArray()).replaceAll("http://www.w3.org/2001/XMLSchema#float","");

但我不能删除 ^^ 部分,因为它们是特殊字符。 如何删除它们?

【问题讨论】:

    标签: java string symbols stardog


    【解决方案1】:

    问题是,如果您在正则表达式中转义这些字符,例如

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

    您在创建的字符串中转义它们。如果这甚至没有错误,你的正则表达式字符串仍然是

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

    所以你需要做的是转义你的反斜杠,将它们变成下面的单个反斜杠,并确保在需要转义特殊正则表达式字符时它们仍然存在:

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

    【讨论】:

    • 没用,不能替代任何东西
    • 您是在转换为 HTML 之前还是之后尝试执行此操作?在 HTML 中,内容将为 ^^&amp;lt;...&amp;gt;,并且需要相应地修改匹配字符串。
    猜你喜欢
    • 2014-05-20
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 2014-03-26
    • 2011-04-11
    • 2016-01-23
    相关资源
    最近更新 更多