tags:

views:

64

answers:

2

I have some text like this.

Every person haveue280 sumue340 ambition

I want to replace ue280, ue340 to \ue280, \ue340 with regular expression

Is there any solution

Thanks in advance

+2  A: 

Something like this?

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

which results in

Every person have\ue280 sum\ue340 ambition

Not sure what you're after, but perhaps it's something like this:

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

(Updated according to axtavt very nice alternative. Making CW.)

aioobe
It doesn't print unicode ;-(
EarnWhileLearn
You mean you want the actual unicode characters, not just putting `\` in front of the unicode notation?
aioobe
yep. does it make sense?
EarnWhileLearn
It works. Great
EarnWhileLearn
A: 

Better version of aioobe's update:

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