mardi 5 mai 2015

How to use regular expression to replace non-digits and math operators together in Java?

How do I only keep chars of [0-9] and [+-/] in a string in Java? My approach is to use a union to create a single character class comprised of [0-9] and [+-/] character classes. But I got an empty string.

public void cleanup(String s){
    String regex = "[^0-9[^+-*//]]";
    String tmp = s.replaceAll(regex, "");
    System.out.println(tmp);
}

Aucun commentaire:

Enregistrer un commentaire