先日の Unicode 文字列ーUTF-8変換をする処理メソッドを整理
これを Java 8 でも動くようにするには、
2年前に書いた、MatchResult の Stream を使うしかないであろう。
oboe2uran.hatenablog.com
↑の MatcherStream を使用して、先日の Unicodes の public String parse(String str) メソッドは、
public final class Unicodes{ private static Pattern ptn2; private static Pattern ptn4; private Unicodes(){ ptn2 = Pattern.compile("\\\\u[0-9a-fA-F]{4}"); ptn4 = Pattern.compile("\\\\\\\\u[0-9a-fA-F]{4}"); }
:
yipuran-core/MatcherStream.java at master · yipuran/yipuran-core · GitHub
import org.yipuran.regex.MatcherStream
public String parse(String str){ if (str==null || str.isEmpty()) return ""; /* for Java 9 String s = ptn4.matcher(str).replaceAll(m-> new String(new int[]{ Integer.parseInt(m.group().substring(3), 16) }, 0, 1) ); return ptn2.matcher(s).replaceAll(m-> new String(new int[]{ Integer.parseInt(m.group().substring(2), 16) }, 0, 1) ); /****************/ AtomicReference<String> r = new AtomicReference<>(str); MatcherStream.findMatches(ptn4, str).forEach(e->{ r.set(r.get().replace(e.group(), new String(new int[]{ Integer.parseInt(e.group().substring(3), 16) }, 0, 1))); }); MatcherStream.findMatches(ptn2, str).forEach(e->{ r.set(r.get().replace(e.group(), new String(new int[]{ Integer.parseInt(e.group().substring(2), 16) }, 0, 1))); }); return r.get(); }