Reputation: 51
I need to parse some text, I am doing this using a Regex expression within the replaceAll() method. This is the line where I use it:
String parsedValue = selectedValue.replaceAll("[^A-Za-z]", "");
This is nearly perfect, it removes the numbers from the string, however it also gets rid of the spaces and I need to keep the spaces? How can I modify it to do this?
For example, "Local Police 101" would become "Local Police".
Upvotes: 1
Views: 1070
Reputation: 442
You're so close! You just need to add a space to your list of "not", so you end up with "[^A-Za-z ]"
;
String parsedValue = selectedValue.replaceAll("[^A-Za-z ]", "");
Notice the space after the lowercase "z" in your regular expression.
Edit:
Looking at your example, you're also wanting to remove the leftover spaces at the beginning and end of the string. To do this, you will also want to trim the result of replaceAll. To do this, simply add .trim()
after replaceAll(). You'll end up with something like this:
String parsedValue = selectedValue.replaceAll("[^A-Za-z ]", "").trim();
Upvotes: 2