I have a problem in that I need to process a list of numbers, which will be in an English sentence. It could be in the following formats:
items 1, 2 and 3
items 2 through 5
items 1 to 20
items 4 or 8
My initial instinct is to write a simple state machine to parse it, but I was wondering if there is any better (simpler) way, such as maybe some regular expression. Any advice?
If you have C++11, the following parser (AXE) will parse all your formats (I didn’t test it):
If you don’t have C++11, you can write a similar parser in C++ using boost::spirit. It’s easier and shorter to write and debug such parser than using regular expressions, and you also get a lot of flexibility in creating parsing rules and semantic actions.