Jonathan Laliberte
Jonathan Laliberte

Reputation: 179

Regex for keeping only letters and blank-space?

This regex works fine for the word variable but it is removing all the white-space from the definition variable which is not wanted. Having trouble understanding why.

String word = "cheese[1]"

String definition = "[:the quality of being too obviously sentimental]"

String regex = "[^A-Za-z]+";       // how to get it to exclude whitespace?

finaldefinition = finaldefinition.replaceAll(regex,"")

Output:

word = cheese

definition = thequalityofbeingtooobviouslysentimental

Desired result:

word = cheese

definition = the quality of being too obviously sentimental

Thanks for your time.

Upvotes: 3

Views: 5911

Answers (2)

slartidan
slartidan

Reputation: 21576

You can either use [^A-Za-z ]+ for:

  • any character, except
    • captial letters
    • lower case letters
    • blanks

Or [^A-Za-z\s]+ for:

  • any character, except
    • captial letters
    • lower case letters
    • whitespaces (blanks, tabs, linebreaks)

Third option: Replace \s to first, so that tabs and linebraks will get blanks

Upvotes: 1

Quinn
Quinn

Reputation: 4504

Are you looking for this?

public static void main(String[] args) {        
    String[] strs = new String[] {"cheese[1]", "[:the quality of being too obviously sentimental]"};
    for (String m: strs){
        System.out.println(m.replaceAll("[^a-zA-Z ]", ""));     
}

Output:

cheese
the quality of being too obviously sentimental

Upvotes: 2

Related Questions