user1228643
user1228643

Reputation: 75

Java regex expression with replace string method

I can't get my head around how regular expressions are done and am trying to get a replace statement to work with one.

I am trying to put a space around everything but numbers and decimals

mathEquation = mathEquation.replaceAll("\\D(?!$)", " $0 ");

That works with everything other than numbers but it still adds spaces around decimals (since the \\D includes decimals). I don't know how to exclude the decimals from the search / replace though.

If someone could help me create the regex I'm looking for or lead me towards the answer I'd appreciate it.

Thanks if you reply.

Upvotes: 1

Views: 217

Answers (1)

Bohemian
Bohemian

Reputation: 424983

Try this:

mathEquation = mathEquation.replaceAll("[^\\d.](?!$)", " $0 ");        

I added a character class for not digits or dot.

Upvotes: 3

Related Questions