0

I've searched a long time, but unfortunately, regex are really not for me...

I just want to replace all the non-alphanumerics except dot or comma between 2 digits (or plus) by a space.

Example : Welcome to RegExr v2.1 by gskinner.com, proudly hosted by Media Temple ! -98.7 3.141 .6180 9,000

became

Welcome to RegExr v2.1 by gskinner com proudly hosted by Media Temple 98.7 3.141 6180 9,000

I know that this can remove all non-alphanumerics : [^a-zA-Z0-9 ] but I just want to add the exception for dot or comma between 2 numbers (I've test on http://regexr.com/)

Thanks for your help ! ;)

Julien M
  • 11
  • 1
  • 3

2 Answers2

8

Try this:

str = str.replaceAll("[^a-zA-Z0-9 .,]|(?<!\\d)[.,]|[.,](?!\\d)", "");

The regex matches

  • everything you definitely don't want, or
  • a dot/comma not preceded by a digit, or
  • a dot/comma not followed by a digit
Bohemian
  • 412,405
  • 93
  • 575
  • 722
2
[a-zA-Z ]|\d([\.,]\d)?

The above will select the text you desire to keep.

It selects either Alphabetic characters + spaces and digits with optionally embedded .'s and ,'s. '|' works as a boolean OR.