简体   繁体   中英

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.

Just add all the allowedChar s 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM