newbie
newbie

Reputation: 43

finding punctuations except for - regex

so I was trying to get rid of words that have punctuations except for -.I was trying to use .match() from string and delete the whole string if it's false. I tried

public static void removeWords(String [] array){
    int i;
    boolean isWords;

    for(i = 0; i < array.length;i++){
        isWords = checkWord(array[i]);
        if (isWords == false)
            array[i] = "";
    }

}

public static boolean checkWord(String word){
    if (word.matches("[a-zA-Z[\\-]]")){
        return true;
    }
    else
        return false;
}

but it won't recognize -.

edit: so "he789llo" should be deleted but "he-llo" shouldn't be.but both words were being deleted by that code

Upvotes: 0

Views: 52

Answers (1)

Kasravnd
Kasravnd

Reputation: 107287

You don't need to put - within another character class also you need to put a + or * after your character class :

[a-zA-Z-]+

Upvotes: 3

Related Questions