in reply to Re^2: Pattern matching
in thread Pattern matching

Unfortunately, what you have provided now is still not a complete spec. You might want any of these things:

So, your first task is to create a tight specification. After that you can devise an algorithm and only then start to consider the coding.