Dawi
Dawi

Reputation: 23

How to exclude a character from regular expression?

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();

Upvotes: 1

Views: 499

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79055

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

Upvotes: 1

Strikegently
Strikegently

Reputation: 2441

I need to check if the word has a hyphen in it but the replace will delete the hyphen

So check if there is a hyphen before you strip non-alpha characters.

if(word.contains("-")) {
    //do whatever
}
//remove non-alpha chars

Upvotes: 0

Related Questions