I have this string with HTML inside: <span title="whatever">something I want to preserve</span>...
I'm using a regex to replace <span title="whatever"> with ( and then the following </span> replace with )
Pattern regex = Pattern.compile("<span\\s+[^>]*title=(['\"])(.*?)\\1[^>]*>");
Matcher matcher = regex.matcher(strLine);
if (matcher.find()) {
strLine = matcher.replaceAll("(");
strLine = strLine.replace("</span>", ")");
}
I works but it replaces all </span> tags; I only want to replace the one that matches the opening tag I just matched.
replaceinstead ofreplaceAll?<span>are being replaced correctly, but too many of the closing tags</span>are being replaced (ie. the ones matching opening tags without title. Is this correct?