@Test public void testReplace() { String src = "$AAA$ some\ns$AAA$b\n$AAA$\n$ SSS$AAA$AAA$\n${B}$"; String exp = "1 some\ns2b\n3\n$ SSS4AAA$\n${B}$\n"; AtomicInteger counter = new AtomicInteger(); StringBuilder sb = new StringBuilder(); for(String line: Splitter.on("\n").split(src)) { sb.append(StringUtils.replace(Pattern.compile("\\$[\\w\\{\\}]+\\$"), line, (s) -> { if(!"$AAA$".equals(s)) { return s; } return Integer.toString(counter.incrementAndGet()); })).append('\n'); } assertEquals(exp, sb.toString()); }
public static String replace(Pattern pattern, String src, Function<String, String> handler) { StringBuilder sb = null; Matcher matcher = pattern.matcher(src); int pos = 0; while(matcher.find()) { if(sb == null) { sb = new StringBuilder(); } String expr = matcher.group(); String replacement = handler.apply(expr); sb.append(src, pos, matcher.start()); sb.append(replacement); pos = matcher.end(); } if(sb == null) { return src; } sb.append(src, pos, src.length()); return sb.toString(); }
StringUtils { public static String replace(Pattern pattern, String src, Function<String, String> handler) { StringBuilder sb = null; Matcher matcher = pattern.matcher(src); int pos = 0; while(matcher.find()) { if(sb == null) { sb = new StringBuilder(); } String expr = matcher.group(); String replacement = handler.apply(expr); sb.append(src, pos, matcher.start()); sb.append(replacement); pos = matcher.end(); } if(sb == null) { return src; } sb.append(src, pos, src.length()); return sb.toString(); } }
StringUtils { public static String replace(Pattern pattern, String src, Function<String, String> handler) { StringBuilder sb = null; Matcher matcher = pattern.matcher(src); int pos = 0; while(matcher.find()) { if(sb == null) { sb = new StringBuilder(); } String expr = matcher.group(); String replacement = handler.apply(expr); sb.append(src, pos, matcher.start()); sb.append(replacement); pos = matcher.end(); } if(sb == null) { return src; } sb.append(src, pos, src.length()); return sb.toString(); } private  StringUtils(); }
StringUtils { public static String replace(Pattern pattern, String src, Function<String, String> handler) { StringBuilder sb = null; Matcher matcher = pattern.matcher(src); int pos = 0; while(matcher.find()) { if(sb == null) { sb = new StringBuilder(); } String expr = matcher.group(); String replacement = handler.apply(expr); sb.append(src, pos, matcher.start()); sb.append(replacement); pos = matcher.end(); } if(sb == null) { return src; } sb.append(src, pos, src.length()); return sb.toString(); } private  StringUtils(); static String before(String s, char c); static String beforeOr(String s, char c, Supplier<String> ifNone); static String after(String s, char c); static String beforeLast(String s, char c); static String afterLast(String s, char c); static String[] splitLast(String s, char c); static String[] splitLast(String s, String delimiter); static String retain(String src, IntPredicate charJudge); static String retainAz09(String src); static String retainForFileName(String src); static boolean isAz09(int cp); static boolean isAz09Hyp(int cp); static boolean isId(int cp); static boolean isHex(int cp); static boolean isForFileName(int cp); static String valueOf(Object o); static boolean match(String str, IntPredicate predicate); static boolean matchAz09(String str); static boolean matchAz09Hyp(String str); static boolean matchId(String str); static boolean matchHex(String str); static String replace(Pattern pattern, String src, Function<String, String> handler); }
StringUtils { public static String replace(Pattern pattern, String src, Function<String, String> handler) { StringBuilder sb = null; Matcher matcher = pattern.matcher(src); int pos = 0; while(matcher.find()) { if(sb == null) { sb = new StringBuilder(); } String expr = matcher.group(); String replacement = handler.apply(expr); sb.append(src, pos, matcher.start()); sb.append(replacement); pos = matcher.end(); } if(sb == null) { return src; } sb.append(src, pos, src.length()); return sb.toString(); } private  StringUtils(); static String before(String s, char c); static String beforeOr(String s, char c, Supplier<String> ifNone); static String after(String s, char c); static String beforeLast(String s, char c); static String afterLast(String s, char c); static String[] splitLast(String s, char c); static String[] splitLast(String s, String delimiter); static String retain(String src, IntPredicate charJudge); static String retainAz09(String src); static String retainForFileName(String src); static boolean isAz09(int cp); static boolean isAz09Hyp(int cp); static boolean isId(int cp); static boolean isHex(int cp); static boolean isForFileName(int cp); static String valueOf(Object o); static boolean match(String str, IntPredicate predicate); static boolean matchAz09(String str); static boolean matchAz09Hyp(String str); static boolean matchId(String str); static boolean matchHex(String str); static String replace(Pattern pattern, String src, Function<String, String> handler);  }