I don’t know how to solve your problem using only tools for working with regular expressions, but you can try to write an analogue of the Matcher.replaceAll method like this:
String myReplaceAll(String pattern, String text, String replacement) { Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); StringBuffer sb = new StringBuffer(); for (int length = 0; m.find(); length = sb.length()) { m.appendReplacement(sb, replacement); if(length > 0) { sb.setCharAt(length, Character.toUpperCase(sb.charAt(length))); } } m.appendTail(sb); return sb.toString(); }
You can do the same without explicitly using classes for working with regular expressions:
String myReplaceAll(String symbol, String text, String replacement) { StringBuilder sb = new StringBuilder(); for (String textChunk : text.split(symbol)) { int prevLength = sb.length(); sb.append(textChunk); if(prevLength > 0) sb.setCharAt(prevLength, Character.toUpperCase(sb.charAt(prevLength))); } return sb.toString(); }
Or, as they wrote in the comments, to write an implementation without regulars, this is especially true if you are not looking for patterns in the string, but for individual characters. For starters, I can offer something like this:
String myReplaceAll(char patternSymbol, char startSkippingSymbol, char stopSkippingSymbol, String text, String replacement) { char[] chars = text.toCharArray(); char[] resultString = new char[chars.length]; boolean upperNext = false, stopSearching = false; for (int index = 0, newIndex = 0; index < chars.length; index++) { char symbol = chars[index]; stopSearching = startSkippingSymbol == symbol ? (stopSearching ? false : true) : stopSearching; if(!stopSearching) { if (upperNext) { symbol = Character.toUpperCase(symbol); upperNext = false; } if (patternSymbol == symbol) { upperNext = true; continue; } } resultString[newIndex++]=symbol; } return new String(resultString); }