Never do in 65 lines of messy string manipulation loops what you can accomplish in 3 lines of RegEx code.
Backreferences rock.
For the curious, I was trying to “englishify” hours of business information, so a set of 14 open/close strings would be translated into stuff like “Mon. - Fri.: 9 - 5″ or “Mon., Wed.: 9 - 5, Fri.: 8 - 4″ or “Mon - Wed., Fri.: 9 - 5, Sat - Sun.: 12 - 6″ or whatever, and the only think that rocks harder than backreferences in a task like this is TestDriven.NET. I was able to write the messiest code ever while testing it out, get it working, and then make it pretty and maintainable. It’s nice to be able to skip step and go straight to the pretty code, but sometimes, particularly with algorithms, it helps to push the bits around the screen for a while.
Technorati Tags: Regular Expressions, string manipulation, TDD
Post a Comment