Wednesday, October 15, 2008

More Splitting!

Since I last posted that handy regex to conditionally split commas, I've migrated to a great native Cocoa emailing engine (Pantomime). I therefore needed to do the splitting in Cocoa and not Java.

Out of the box, you can't split an NSString by matching a RegEx pattern. I found RegExKit Lite on SourceForge and threw the old RegEx at it. Unfortunately, the pattern matching was not reliable. But folks speak highly of this framework, and it's BSD, so I needed to give it a good try before moving on.

I am not sure if it is was a problem with RegExKit or the fact that it is ICU and not PCRE based... but I went looking for another pattern that would work. I must have tried a dozen before I found Programmer's Heaven's solution. The pattern is actually simpler and works perfectly on NSStrings with RegExKitLite v 2.1.

The original posted by Jonathan was :
,(?!(?:[^",]|[^"],[^"])+")

And with the double quotes escaped and ready for NSString CSV duty it looks like this: