Java Unicode正規表現
-
04-10-2019 - |
質問
このようなテキストがあります。
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();
所属していません StackOverflow