TextRange (String docString, int pos, String[] paramTags) { int result = Integer.MAX_VALUE; String foundTag = null; for (String paramTag : paramTags) { int tagPos = docString.indexOf(paramTag, pos); while(tagPos >= 0 && tagPos + paramTag.length() < docString.length() && Character.isLetterOrDigit(docString.charAt(tagPos + paramTag.length()))) { tagPos = docString.indexOf(paramTag, tagPos+1); } if (tagPos >= 0 && tagPos < result) { foundTag = paramTag; result = tagPos; } } return foundTag == null ? null : new TextRange(result, result + foundTag.length()); }