Question

J'ai un texte comme celui-ci.

Every person haveue280 sumue340 ambition

Je veux remplacer ue280, ue340 à \ ue280, \ ue340 avec l'expression régulière

Y at-il une solution

Merci à l'avance

Était-ce utile?

La solution

Quelque chose comme ça?

String s = "Every person haveue280 sumue340 ambition";

// Put a backslash in front of all all "u" followed by 4 hexadecimal digits
s = s.replaceAll("u\\p{XDigit}{4}", "\\\\$0");

qui se traduit par

Every person have\ue280 sum\ue340 ambition

Je ne sais pas ce que vous êtes après, mais peut-être de quelque chose comme ceci:

static String toUnicode(String s) {
    Matcher m = Pattern.compile("u(\\p{XDigit}{4})").matcher(s);
    StringBuffer buf = new StringBuffer();
    while(m.find())
        m.appendReplacement(buf, "" + (char) Integer.parseInt(m.group(1), 16));
    m.appendTail(buf);
    return buf.toString();
}

(Mise à jour selon axtavt alternative très agréable. Faire CW.)

Autres conseils

meilleure version de la mise à jour aioobe:

String in = "Every person haveue280 sumue340 ambition";

Pattern p = Pattern.compile("u(\\p{XDigit}{4})");
Matcher m = p.matcher(in);
StringBuffer buf = new StringBuffer();
while(m.find()) 
    m.appendReplacement(buf, "" + (char) Integer.parseInt(m.group(1), 16));
m.appendTail(buf);
String out = buf.toString();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top