String (String referenceText) { int stack = 0; for (int i = referenceText.length() - 1; i >= 0; i--) { char c = referenceText.charAt(i); switch (c) { case '<': stack--; break; case '>': stack++; break; case '.': if (stack == 0) return referenceText.substring(0, i); } } return ""; }