simPod
simPod

Reputation: 13456

Java - replaceAll by regex, then replace first occurance of string

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.

Upvotes: 1

Views: 1561

Answers (3)

Bart Kiers
Bart Kiers

Reputation: 170148

Why not do it in one replaceAll(...) call:

String s = "noise <span title=\"whatever\">something I want to preserve</span>...";
s = s.replaceAll("<span\\s+[^>]*title=(['\"])(.*?)\\1[^>]*>(.*?)</span>", "($3)");
System.out.println(s);

which will print:

noise (something I want to preserve)...

EDIT

Note Alan's comment under my answer: this assumes you don't have nested <span>'s in your input.

Upvotes: 6

bpgergo
bpgergo

Reputation: 16037

Instead of replacing the <span> tags, you could try to extract the content of the <span> tag and then wrap it with braces.

Upvotes: 1

John B
John B

Reputation: 32949

I suggest you use a single regex for matching the entire <span ...>...</span>. Capture the <span> in one group and the </span> in another and use the capture groups to do the replacement.

Upvotes: 3

Related Questions