userit1985
userit1985

Reputation: 993

How to remove all non-alphanumeric characters from a string expect decimal point in Java

Having this String with decimal point, I would like to remove all non alphaNumeric expect the decimal point.

 String toPharse = "the. book - cost 7.55 dollars.";

 String newPharse =  toPharse.replaceAll("[^A-Za-zd.0-9 ]", " ").replaceAll("\\s+", " ");

Currently I get "the. book cost 7.55 dollars.";

However I would like to return "the book cost 7.55 dollars";

Upvotes: 1

Views: 180

Answers (1)

anubhava
anubhava

Reputation: 785128

You can use:

String toPharse = "the. book - cost 7.55 dollars.";
toPhrase = toPharse
   .replaceAll("(?<!\\d)\\.(?!\\d)|[^a-zA-Z\\d. ]+", "")
   .replaceAll("\\h{2,}", " ");

//=> "the book cost 7.55 dollars"

RegEx Demo

RegEx Details:

  • (?<!\\d): Previous character is not a digit
  • \\.: Match a dot
  • (?!\\d): Next character is not a digit
  • |: OR
  • [^a-zA-Z\\d. ]+: Match 1+ of non-alphanumeric characters that are not space or dot
  • .replaceAll("\\h{2,}", " "): is for replacing 2+ whitespaces with a single space

Upvotes: 3

Related Questions