Java Unicode正则表达式

Java Unicode正则表达式,java,regex,character-properties,Java,Regex,Character Properties,我有一些这样的文字 Every person haveue280 sumue340 ambition 我想用正则表达式替换ue280、ue340到\ue280、\ue340 有什么解决办法吗 提前谢谢诸如此类的事 String s = "Every person haveue280 sumue340 ambition"; // Put a backslash in front of all all "u" followed by 4 hexadecimal digits s = s.repl

我有一些这样的文字

Every person haveue280 sumue340 ambition
我想用正则表达式替换ue280、ue340到\ue280、\ue340

有什么解决办法吗

提前谢谢诸如此类的事

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");
导致

Every person have\ue280 sum\ue340 ambition

不确定你在追求什么,但也许是这样的:

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

(根据axtavt非常好的替代方案进行更新。制作CW。)

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();

你的意思是你想要的是实际的unicode字符,而不仅仅是在unicode符号前面加上“`”?