January 15, 2007

Programming Challenge Three

So, I haven't posted my solution to last weeks puzzle, for a couple of reasons. The big one is that my solution doesn't work yet. I successfully solve a problem, but on closer inspection, it was quite a different one from the one that I posed. And in fact, I can't solve the problem I posed the way I approached it. Thus I need to start over. And I will. But I can't miss this weeks challenge, or I'm hosed.

This week, I'll going for something slightly simpler, though it has virtually infinite room for expansion and optization.

Challenge Three

Write a pattern matching engine. Given a pattern and a string, return a boolean match or no match response:

matches = pmatch( pattern, inputstr );

The pattern syntax can be anything other than straight substrings, though you must start by selecting a subset of the PCRE syntax. Start with the '*', '+' and '?' quantifiers, together with character classes and alternations. Ignore capturing, but include grouping. That set of features is probably the minimum set to provide useful matching facilities, but it's not so overwhelming as to require enormous amounts of code. Add to those features as long as it remains interesting.

Some random notes:

It may seem foolish to work at solving a problem so widely and commonly solved, but this is in fact a challenging problem with lots to think about. If you work with regular expressions every day, take this opportunity to look at them from the bottom.