lundi 24 août 2015

how would you sanitize '$' and '/' in string (java)

Having difficulty sanitizing the string to allow $ and / to stay within the string. Using the Pattern class and I'm not sure if I should place the

import java.util.regex.Pattern;

public String customiseText(String bodyText, List<Object> objectList) {
    Map<String, String> replaceKeyMap = extractMapFromList(objectList);
    String escapedString = Pattern.quote(bodyText); //
    // iterate over the mapkey and return the body text replace
    for (final String key : replaceKeyMap.keySet()) {
        String replacementKey = "(?i)" + key; // not case sensitive and empty string matcher
        String replacementValue = replaceKeyMap.get(key);
        if (replacementValue == null) {
            replacementValue = "";
        }
        escapedString = escapedString.replaceAll(replacementKey, replacementValue);
    }

    return escapedString;
}

Aucun commentaire:

Enregistrer un commentaire