简体   繁体   中英

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

您不需要将-放在另一个字符类中,也不需要在字符类后放置+*

[a-zA-Z-]+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM