I need a way to fix capitalization in abbreviations found within a String
. Assume all abbreviations are correctly spaced.
For example,
"Robert a.k.a. Bob A.k.A. dr. Bobby"
becomes:
"Robert A.K.A. Bob A.K.A. Dr. Bobby"
Correctly capitalized abbreviations will be known ahead of time, stored in a Collection
of some sort.
I was thinking of an algorithm like this:
private String fix(String s) {
StringBuilder builder = new StringBuilder();
for (String word : s.split(" ")) {
if (collection.contains(word.toUpperCase()) {
// word = correct abbreviation here
}
builder.append(word);
builder.append(" ");
}
return builder.toString().trim();
}
But as far as I know, there are a couple of problems with this approach:
- If the abbreviation has a lower case letter (Dr.)
- If the word starts or ends with punctuation ("a.k.a.")
I have a feeling that this can be solved with a regex, iteratively matching and replacing the correct abbreviation. But if not, how should I approach this problem?