string replacement - regex?

G

Guest

Hi & help,

I'm trying to parse arithmetic expressions such as 4*(7-2.1).

The first thing I'm trying is to add spaces to get: 4 * ( 7 - 2.1 ).

I thought that Regular Expressions would be the way to go. I tried:
regex.replace("\D", " \D ") which did not work. I don't know how to refer
to the string that was matched in forming the replacement.

Also, I tried regex.replace("+*", " +* ") This was even worse because I'm
apparently not using a recognizable syntax.

Did I mention that this is my first attempt at regular expressions?

I'd really appreciate any suggestions.

Art
 
R

Rick Mogstad

In order to use "tagged expressions", you need to use the following format.
I am not too awful good with regex either, but there is a good tool called
Regex Builder that I always use to help out with my Regular Expressions.
So, in short, I cant help you with your problem necessarily, but the Tagged
expression format is as follows...


regex.replace("\D", " \D ")

to

regex.replace("{\D}", " \1 ")
 
G

Guest

Thanks Rick -- I will give that a try.

Art

Rick Mogstad said:
In order to use "tagged expressions", you need to use the following format.
I am not too awful good with regex either, but there is a good tool called
Regex Builder that I always use to help out with my Regular Expressions.
So, in short, I cant help you with your problem necessarily, but the Tagged
expression format is as follows...


regex.replace("\D", " \D ")

to

regex.replace("{\D}", " \1 ")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top