On the surface, this doesn't appear to be too difficult a task. In general, I use regular expressions for matching operations. Regular expressions are quite powerful. Because they are so powerful, they are also somewhat complex. You might start by checking out the following link:
If you have further, specific questions, I'll be happy to assist.
- Roger Nedel