static String unpack(String source) { Matcher m = Pattern.compile("(\\w)(\\d)*").matcher(source); return Stream .generate(() -> m.find() ? Pair.of(m.group(1), m.group(2)) : null) .takeWhile(s -> s != null) .flatMap(s -> IntStream.range(1, ("".equals(s.snd) ? 1 : Integer.parseInt(s.snd))).mapToObj(a -> s.fst)) .collect(Collectors.joining()); }