1

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();
maloomeister
  • 2,461
  • 1
  • 12
  • 21
Dawi
  • 23
  • 3
  • checkout https://stackoverflow.com/questions/49510006/remove-and-other-such-emojis-images-signs-from-java-strings – riorio Nov 11 '20 at 14:30

2 Answers2

1

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
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

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
Strikegently
  • 2,251
  • 20
  • 23