Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Try this:

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

I added a character class for not digits or dot.

share|improve this answer
    
Thanks, worked perfectly. Appreciate the help. –  user1228643 Mar 28 '12 at 23:06
    
Please accept the answer it solved your problem. –  kirdie Mar 28 '12 at 23:23
    
Oh, sorry. Accepted it now. –  user1228643 Mar 28 '12 at 23:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.