Idea of "a s d" as acronym was just an example. You can take "NATO as an example". Now somewhere in text, someone writes it as "hallo N. A. T. O. java world", which he meant it to be "N.A.T.O.". In such cases what I want is to find such
patterns and get the whole picture "N.A.T.O.". in my earlier example I had removed dot for ease of use. If dot is present, my pattern will look like "(\\s+)(([A-Z](?:\\.?)(?:\\s+)){2,})". Here my intention is a pattern starting with one ore more whitespace, followed by two or more repetition of combination of character followed by dot (one or not at all), followed by one or more whitespace. last two(dot and whitespace) are non-captured groups.
NB- please do not focus on that in previous post I've not supplied dot handling and lower/upper case handling, those are not the issues
My code looks like this:
private static final String COMPACT_ACRONYMS = "(\\s+)(([A-Z](?:\\.?)(?:\\s+)){2,})";
public static String compactSpacedAcronyms(String text){
Pattern p = Pattern.compile(COMPACT_ACRONYMS);
Matcher m = p.matcher(text);
text = m.replaceAll("$1$3)");
return text;
}
This code matches the pattern correctly, however for replacement, I need some trick to compact the acronym. Here, I understand that it finds 'N.', 'A.', 'T.', 'O.' individually. however overrides the previous findings and at last '$3' prints 'O.' only. Is there any way to print 'N.A.T.O.' so that my final text becomes "hallo N.A.T.O. java world".