1

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.

1 Answer 1

3

Try this:

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

I added a character class for not digits or dot.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.