Erro no tratamento de caracteres especiais

1 resposta
V

Pessoal,

Estou usando essa função para utilização de caracteres especiais:

private String HTMLfy(String s) {
        String str = s.replaceAll("<span class=\"msg_sender\">", "").replaceAll("</span>", "").replaceAll("<br>", "\n");
        str = str.replaceAll("ã", "&atilde").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("õ", "&otilde;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("á", "&aacute;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("é", "&eacute;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("í", "&iacute;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ó", "&oacute;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ú", "&uacute;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ý", "&yacute;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("â", "&acirc;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ê", "&ecirc;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ô", "&ocirc;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("î", "&icirc;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("û", "&ucirc;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ç", "&ccedil;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("à", "&agrave;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("è", "&egrave;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ì", "&igrave;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ò", "&ograve;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ù", "&ugrave;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ù", "&Ugrave;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ò", "&Ograve;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ì", "&Igrave;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("È", "&Egrave;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("À", "&Agrave;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ä", "&auml;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ë", "&euml;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ï", "&iuml;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ö", "&ouml;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ü", "&uuml;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ü", "&Uuml;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ö", "&Ouml;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ï", "&Iuml;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ë", "&Euml;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ä", "&Auml;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ÿ", "&yuml;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ñ", "&ntilde;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ñ", "&Ntilde;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ã", "&Atilde;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Õ", "&Otilde;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Á", "&Aacute;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("É", "&Eacute;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Í", "&Iacute;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ó", "&Oacute;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ú", "&Uacute;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ý", "&Yacute;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Â", "&Acirc;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ê", "&Ecirc;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ô", "&Ocirc;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Î", "&Icirc;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Û", "&Ucirc;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("Ç", "&Ccedil;").replaceAll("\n", "<br>").replaceAll("\r", "");              
        str = str.replaceAll("¨", "&uml;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("§", "&sect;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("ª", "&ordf;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("º", "&ordm;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("°", "&deg;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("´", "&acute;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("%", "&#37;").replaceAll("\n", "<br>").replaceAll("\r", "");
        str = str.replaceAll("&", "%26").replaceAll("+", "%2B").replaceAll("#", "%23");
        return str;
    }//HTMLfy

quando tento enviar me retorna esse erro:

java.util.regex.PatternSyntaxException: Dangling meta character + near index 0

+

^

at java.util.regex.Pattern.error(Pattern.java:1650)

at java.util.regex.Pattern.sequence(Pattern.java:1787)

at java.util.regex.Pattern.expr(Pattern.java:1687)

at java.util.regex.Pattern.compile(Pattern.java:1397)

at java.util.regex.Pattern.(Pattern.java:1124)

at java.util.regex.Pattern.compile(Pattern.java:817)

at java.lang.String.replaceAll(String.java:2000)

at br.com.cpqd.www.dmd.chat.ChatFrame.HTMLfy(ChatFrame.java:322)

at br.com.cpqd.www.dmd.chat.ChatFrame.jButton1ActionPerformed(ChatFrame.java:183)

at br.com.cpqd.www.dmd.chat.ChatFrame.access$000(ChatFrame.java:31)

at br.com.cpqd.www.dmd.chat.ChatFrame$1.actionPerformed(ChatFrame.java:82)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)

at java.awt.Component.processMouseEvent(Component.java:5488)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)

at java.awt.Component.processEvent(Component.java:5253)

at java.awt.Container.processEvent(Container.java:1966)

at java.awt.Component.dispatchEventImpl(Component.java:3955)

at java.awt.Container.dispatchEventImpl(Container.java:2024)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)

at java.awt.Container.dispatchEventImpl(Container.java:2010)

at java.awt.Window.dispatchEventImpl(Window.java:1774)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Alguém pode me ajudar?

1 Resposta

hashcode
str = str.replaceAll("&", "%26").replaceAll("+", "%2B").replaceAll("#", "%23");

para

str = str.replaceAll("&", "%26").replaceAll("\\+", "%2B").replaceAll("#", "%23");

Escape todos os carateres de expressão regular

. ? ^ [ ] ( ) * \b etc
Criado 19 de julho de 2006
Ultima resposta 19 de jul. de 2006
Respostas 1
Participantes 2