I'm currently working on a project where I am creating a parser for arithmetic expressions. I want to add some 'high end' functionality, but have an issue tied to regular expressions. I am relatively new to regular expressions, so I am not 100% certain its the right tool for this below case.
The aim is to have our users create an equation string for a calculation field (assisted by a GUI interface, so they don't have to format the string raw) that will then sit as a new field against our data presentation model for information we are storing in a CSV format in our database. However, I would like the user to be able to draw named fields from their data model into their calculations.
As an example, take the below example model.
- Name (string - raw data - column 1)
- Temp_in_degrees (number - raw data - column 2)
- Speed (number - raw data column 3)
Say we want to add a new field to it, for temperature in Fahrenheit
- Name (string - raw data - column 1)
- Temp_in_degrees (number - raw data - column 2)
- Speed (number - raw data column 3)
- Temp_in_fahrenheit (number - calculated)
I have the string of the equation for field 4 as below. Notice it is referring to the value of field 2 in the raw data as part of the equation, enclosed in the parenthesis.
(({Temp_in_degrees} * 9) / 5) + 32
This is a very simple sample equation. Examples of higher complexity may include more than one reference to existing raw data fields.
Now, I would like to use regular expressions to look at a string like this, and to return all sub-strings within that string that happen to appear between the "{" and the "}". In our first example equation above, I would like to return Temp_in_degrees. In a much more complex (but nonsense) example:
({Temp_in_degrees} / {Speed}) * 30;
I would like to return Temp_in_degrees and Speed (or, loop through and handle each string as it is found)
Because of the users ability to customise their models, I cannot assume we are always going to have strings of the same name.
I do have an alternate idea for solving this issue, where I use String.indexOf to resolve it by looping the string, getting indexes, getting substrings between those indexes, then substituting values from our raw data into the string and repeat, but feel this is something RegEx should be able to help with.
Any ideas?