Categories


Archives


Recent Posts


Categories


Quick Reg. Exp. for Cleaning Up Text Files

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Often, when dealing with text files in my day-to-day duties, I’ll end up with large gaps of newlines and white space that I want to zap. Something line

this is a line

this is another line


This is, yet another, line.
[this is an invisible tab chacater]

More lines!

A decade ago I’d spend tedious/precious minutes going through the file manually and cleaning up the extra lines. Now I just search on the following regular expression and replace it with a single newline

\n[\n\s]+

Or, broken out /x style

\n          #a new-line character 
[\n\s]+     #a character class consisting of any 
            #newline character (\n) or white space (\s), 
            #repeated one or more times (+)

Keep in mind if you’re using BBEdit, you’ll need to use \r instead of \n, since Bare Bones interpreted Apple’s lack-of-official documentation on OS X’s line endings to mean “keep using \r”.

Originally published May 13, 2007