Ashesh Nair
Ashesh Nair

Reputation: 327

Parsing special character using Java Regex

I have a requirement where i need to remove those special characters from a string which are not in the array list. The current code removes all special character when found ,

String Modified_remark = final_remark.replaceAll("[^\\x00-\\x7F]", "");

This code removes all special character from the string , But i want to retain certain items like Angstrom Symbol (Å) & Micron Symbol (μ)

For example if i place the allowed special character in Array , i want the code to skip the replacement and if not matching then replace with "" (Empty quotes).

String[] allowedChar = {Å, μ};

To be added more when requested by User's. Can anyone help with this logic.

Upvotes: 0

Views: 388

Answers (1)

Dmitry Egorov
Dmitry Egorov

Reputation: 9650

Just add all the allowedChars to the exception list in your regex:

final_remark.replaceAll("[^\\x00-\\x7F" + String.join("", allowedChar) + "]", "");

Demo: https://ideone.com/iQWvHI

Update

As Wiktor Stribiżew rightly pointed out, the this simple code breaks if allowedChar contains some regex special characters. Since the requirements imply allowedChar to contain only non-ACSII characters, we may add a condition on allowedChar as follows:

String[] allowedChar = {"Å", "μ", "]"};
String allowedChars = "";
for (String ch : allowedChar)
    if (ch.matches("^[^\\x00-\\x7F]$"))
        allowedChars += ch;
String Modified_remark = final_remark.replaceAll("[^\\x00-\\x7F" + allowedChars + "]", "");

Demo: https://ideone.com/94513e

Upvotes: 2

Related Questions