Designing the parser
We need to pause here before we dive into matching regular expressions against text. We need to think about what our strategy will be for searching. We really have two options:
- We could combine the regular expressions into a single pattern and perform an iterative search through each string to find all the matches, and then disentangle the results after the fact
- We could parse the string several times, with each pass looking for a different pattern, keeping the results separate
The first strategy has the advantage of speed, especially if the strings are long. The second strategy has better separation of concerns, which allows us to focus on each component at a time. At the start of the section, we said we would use the second strategy. The main reason for this is that we can write the code much more quickly since it dramatically simplifies the process of unpicking the various groups into the numbers we need.
To facilitate the process...