Yesterday I’ve subscribed to Rubies in the Rough from James Edward Gray II and read his article “Doing it Wrong”.
In his article he questions (along with some other rules) the rule of never using regular expression for xml parsing.
As it turned out it was a fortunate move since six dollars and one day latter I came across .xml that needed to be parsed.
I have a confession to make: I’ve always hated xml parsers. This particular .xml did not even use xml strengths; data inside was all messed up:
In the article he made a good point and illuminated the edge cases where you are not really parsing, but rather just hunting for some data.
That was encouraging enough to end up with this:
Does it feel good? It sure does. Is it any worse than using xml parser? No, I don’t think so. Data is safe and sound, just look how happy it looks:
I didn’t really break any rules. I just saw the whole problem more clearly because of his article. I figured I would need the regex anyhow since document was structured so unfortunately.
Other then recommending his articles I can say that being pragmatic can get you solutions that are on the other side of rules fence.
The thing is that sometimes the other side doesn’t have to be a bad side.