AHungerArtist
AHungerArtist

Reputation: 9599

Remove specific characters inside parentheses using regex

I have a line like this:

BlockedMatch(XA, YB), Correlation(XA, QC), Correlation(YB, QC), Correlation(QC, YB)

I want it to look like this:

BlockedMatch(XAYB), Correlation(XAQC), Correlation(YBQC), Correlation(QCYB)

I can't just do a replace on ", " because it will remove those instances that exist outside of the parentheses.

I tried this:

replaceAll("\\((.*?)\\)", "")

which replaces everything inside the parentheses (not just the comma). I've tried to add just the comma and space combination into that regex, but it doesn't seem to remove anything then.

Could someone show me how to specify to only remove the ", " (comma-space) when it occurs inside parentheses?

Upvotes: 3

Views: 2441

Answers (4)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89639

You can do it with a simple word-boundary since the commas you want to avoid are preceded by a closing parenthesis and the commas you want to match are preceded by a letter:

str = str.replaceAll("\\b, ", "");

Upvotes: 1

Bohemian
Bohemian

Reputation: 425348

Use a look ahead:

str = str.replaceAll(", (?=[^(]*\\))", "");

This regex says "replace the comma-space only when the next bracket character is a close bracket"


Some test code:

String str = "BlockedMatch(XA, YB), Correlation(XA, QC), Correlation(YB, QC), Correlation(QC, YB)";
str = str.replaceAll(", (?=[^(]*\\))", "");
System.out.println(str);

Output:

BlockedMatch(XAYB), Correlation(XAQC), Correlation(YBQC), Correlation(QCYB)

Upvotes: 2

OnlineCop
OnlineCop

Reputation: 4069

The safest way to do this is to use two regexes: First, capture all (...) and from those results, remove all commas and optional whitespace.

For your specific case, you can search , *([^()]*)(?=\)) and replace with \1 which you can see here.

This may have issues with edge cases where you have multiple things within your parentheses that you wish to remove (such as (XA, YB, ZC)).

Or (without replacing) search for , *(?=[^(]*\)) and replace with (nothing) which you can see here. This handles multiple , fairly well, but will have problems if you have embedded (...) characters.

Upvotes: 2

anubhava
anubhava

Reputation: 786091

You can use:

String s = "BlockedMatch(XA, YB), Correlation(XA, QC), Correlation(YB, QC), Correlation(QC, YB)";
Pattern p = Pattern.compile("(\\([^)]+\\))");
Matcher mat = p.matcher(s);
StringBuffer sb = new StringBuffer();
while (mat.find()) {
    mat.appendReplacement(sb, mat.group(1).replaceAll(" *, *", ""));
}
mat.appendTail(sb);
System.out.println(sb);

Output:

BlockedMatch(XAYB), Correlation(XAQC), Correlation(YBQC), Correlation(QCYB)

Upvotes: 1

Related Questions