How to exclude a character from regular expression?

Tags:



I want to replace all non words characters from a string but I need to check if the word has a hyphen in it but the replace will delete the hyphen . is there a way to do that after I replace everything that is not a letter or do I have to check before replacing ?

this is my code

word = word.replaceAll("[^a-zA-Z]", "").toLowerCase();

Answer

Use the regex, [^w-] which means NOT(a word character or -).

public class Main {
    public static void main(String[] args) {
        // Test
        String word = "Hello :) Hi, How are you doing? The Co-operative bank is open 2day!";
        word = word.replaceAll("[^\w-]", "").toLowerCase();
        System.out.println(word);
    }
}

Output:

hellohihowareyoudoingtheco-operativebankisopen2day

Note that a word character (i.e. w) includes A-Za-z0-9_. If you want your regex to restrict only up to alphabets and hyphen, you should use [^A-Za-z-]

public class Main {
    public static void main(String[] args) {
        // Test
        String word = "Hello :) Hi, How are you doing? The Co-operative bank is open 2day!";
        word = word.replaceAll("[^A-Za-z\-]", "").toLowerCase();
        System.out.println(word);
    }
}

Output:

hellohihowareyoudoingtheco-operativebankisopenday


Source: stackoverflow