Searching for tags in a string
Hi everyone, sorry that my first post here is a question but I've been struggling with this for a while so any help would be greatly appreciated.
I'm working on a project that requires my program to read a question from a text file, and then convert this question into a mathematical equation that can be calculated.
An example from this questions.txt is
As you can see certain areas have been surrounded by parentheses. These parts will need to be identified as mathematical objects and then added to the equation, while the areas outside these tags are stored in another string. The ints will be replaced by randomly generated integers after the question is read through.
What is [int] [+] [int]?
In short, after scanning through a question, there should be two strings. One containing a question written in english ("What is 2 + 2?"), and another written as a pure equation(2+2).
My issue is getting the program to recognize these tagged areas and convert them into the equation friendly characters I need. So far it seems the best way to do this is to convert the entire string into a char list and scan through one char at a time, but I'm struggling to keep it simple and I'm reaching a ridiculous amount of nested loops.
If anyone has any ideas it'd really be appreciated. Sorry for such a specific and wordy question, I'm sure it could've been explained better :s:
Re: Searching for tags in a string
Regular expressions might be just what you need. They allow you to find sequences of characters that follow a specified pattern.
Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
If you start getting into mathematical expressions with nested parentheses and stuff, it will become much more complicated. But a regexp could easily identify a string like "2+2".