String (String word) { if (word.trim().isEmpty()) return word; String result = word; if (word.startsWith(" ")) result = "(?:\\s|\\b)" + result.substring(1); if (word.endsWith(" ")) result = result.substring(0, result.length() - 1) + "(?:\\s|\\b)"; return result; }