Reputation: 179
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
Reputation: 21576
You can either use [^A-Za-z ]+
for:
Or [^A-Za-z\s]+
for:
Third option: Replace \s
to first, so that tabs and linebraks will get blanks
Upvotes: 1
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