I want to format the line:

10% go to school, which is free of charge.

and get the following output:

About 90 percent of all schools and schools that are free ...

It turned out to replace the first letter in the expression with a big one, I can not figure it out anymore. Code:

private static final String STRING = "about 90 percent of all children attend public school, which is free ... the other 10 percent go I private schools,Which often include religious education."; private static final String REGEX = "(?:^| )^[az]"; public static void main(String[] args){ Matcher matcher = Pattern.compile(REGEX).matcher(STRING); StringBuffer stringBuffer = new StringBuffer(); while (matcher.find()){ matcher.appendReplacement(stringBuffer, matcher.group().toUpperCase()); } matcher.appendTail(stringBuffer); System.out.println(stringBuffer.toString()); } 
  • one
    Explain what you need to replace with what algorithm. - Enikeyschik
  • @ Enikeyschik, in fact you just need to edit the line. 1) A capital letter after a point, all other capital letters. 2) Remove spaces before separators (add after). - UjinUkr

2 answers 2

Works if the text is in Latin:

  String s = "about 90 percent of all children attend public school, which is free ... the other 10 percent go I private schools,Which often include religious education. `"; Pattern pattern = Pattern.compile("\\.\\s[az]|,[AZ]|\\s`|\\s\\.{3}\\s"); Matcher matcher = pattern.matcher(s); while (matcher.find()){ String item = matcher.group(); s = item.matches("\\.\\s[az]") ? s.replaceAll(item, item.toUpperCase()) : item.matches(",[AZ]") ? s.replaceAll(item, item.substring(0,1).concat(" ").concat(item.substring(1,2).toLowerCase())) : item.matches("\\s`") ? s.replaceAll(item, "") : item.matches("\\s\\.{3}\\s") ? s.replaceAll("\\s\\.{3}\\s", "... ") : ""; } s = s.substring(0, 1).toUpperCase() + s.substring(1); 

About 90 percent of I go to private schools, which is free of charge.

  • Before the ellipsis, the space character remains. - UjinUkr
  • And you yourself do not want to work on this? - alex
  • I added an option with dots - alex
  • I copied your code and compiled it and got the following: 10 percent go I have private schools, which often include religious education. - UjinUkr
  • Yes. strange. I can not understand what the problem is - alex

Having a little understood wrote the necessary functionality. It is written karyavo, so I will be glad if you specify how you can simplify and optimize the code.

 private static final String COMA_REGEX = "\\s*,\\s*"; private static final String DOT_REGEX = "\\s*(?<!\\.)\\.(?!\\.)\\s*"; private static final String MULTI_DOT_REGEX = "\\s*\\.{3}\\s*"; private static final String APOSTROPHE_REGEX = "\\s*'\\s*"; private static final String FORMAT_CASE = "((?:^| )^\\w" + "|" + MULTI_DOT_REGEX + "\\w" + "|" + DOT_REGEX + "\\w)"; private static final String COMA_REPLACEMENT = ", "; private static final String MULTI_DOT_REPLACEMENT = "... "; private static final String DOT_REPLACEMENT = "."; private static final String APOSTROPHE_REPLACEMENT = "'"; public static void main(String[] args){ String buffer = ""; buffer = SECOND_STRING.replaceAll(COMA_REGEX, COMA_REPLACEMENT); buffer = buffer.replaceAll(DOT_REGEX, DOT_REPLACEMENT); buffer = buffer.replaceAll(MULTI_DOT_REGEX, MULTI_DOT_REPLACEMENT); buffer = buffer.replaceAll(APOSTROPHE_REGEX, APOSTROPHE_REPLACEMENT); buffer = getFormatCaseString(buffer); System.out.println(buffer); } 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(); }