Java Unicode Regular Expression
-
04-10-2019 - |
Domanda
ho un po 'di testo come questo.
Every person haveue280 sumue340 ambition
Voglio sostituire ue280, ue340 a \ ue280, \ ue340 con espressioni regolari
C'è qualche soluzione
Grazie in anticipo
Soluzione
Una cosa come questa?
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");
che si traduce in
Every person have\ue280 sum\ue340 ambition
Non sei sicuro di quello che stai dopo, ma forse di qualcosa di simile a questo:
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();
}
(Stato in base alla axtavt molto bella alternativa. Fare CW.)
Altri suggerimenti
Una migliore versione di aggiornamento di 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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow