How would i replace a letter/number after a symbol in replaceAll

Viewed 125

title explains everything. I'm wondering how i can replace ONE letter/number after this specific symbol § using the replaceAll method.

Any help is appreciated, I'm not really good with regex.

I can replace the § with nothing, but that doesn't really help, see:

public String removeSymbols(String text) {
        text.replaceAll("§", "");
        return text;
    }
Before replace all:

§f§c§lTa§6§lpp§e§lle §7- §8[§61.8§8]
§c§lBLACK FRIDAY SALE §7- §e§lUP TO 40% OFF

After:

1f1c1lTa161lpp1e1lle 17- 18[161.818]
1c1lBLACK FRIDAY SALE 17- 1e1lUP TO 40% OFF

Like i said, any help is appreciated. thanks!

1 Answers

Use

public String removeSymbols(String text) {
    return text.replaceAll("(?<=§)\\p{Alnum}", "");
}

Explanation

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    §                        '§'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \p{Alnum}                 any ASCII letter or digit
Related