I want to check the line for the presence of randomly exposed letters of different registers. For example, the input line is:
This is a line ... this is TroCa.
Output line:
This is a string ... This is a string.
I install the first letter of the sentence like this:
private static final String DOT_REGEX = "\\s*(?<!\\.)\\.(?!\\.)\\s*"; private static final String MULTI_DOT_REGEX = "\\s*\\.{3}\\s*"; private static final String FORMAT_CASE = "(?:^| )^\\w" + "|" + MULTI_DOT_REGEX + "\\w" + "|" + DOT_REGEX + "\\w"; The function that processes the string and returns the finished result:
private static String getFormatCaseString(String targetString){ Matcher matcher = Pattern.compile(FORMAT_CASE).matcher(targetString); StringBuffer stringBuffer = new StringBuffer(); while (matcher.find()){ matcher.appendReplacement(stringBuffer, matcher.group().toUpperCase()); } matcher.appendTail(stringBuffer); return stringBuffer.toString(); } The idea is to split the sentence into two groups: the first is the first letters of the new sentence (which are set in UpperCase ) and the second is all the other letters that are in lower case. How to do it?