Andrew Maddison

Flowerchild.

Regex to Find \n Without \r

I recently had a problem with the IIS6 SMTP server on one of our sites. We were getting lots of script kiddie attackes, which happened to trigger an exception which in turn fired of squillions of Error emails. After a while the SMTP server seemed to loose the will to live and quietly coughed and then died.

It turned out (Diagnosed thanks to this blog post from Dylan Beattie) that SMTP doesn’t strictly allow a \n (newline) without an \r (carriage return). I’d released a new bit of code that splatted precisely these characters into a nice verbose error email, designed to help me figure out what our script kiddie was up to. Long story short, this broke the smtp server (read Dylan’s post for details).

Anyway, this lead me to want to find any occurences of \n in string literals in all of my code. I assumed I could do this using Visual studio’s Find in Files with the Regex box ticked. A bit of regex wrangling ensued, but we think we settled on the following expression:

~(\r)\n

Which seemed to do the trick. At some point we may modify our email code to search for lone \n characters in message bodies, in which case the same regex could be modified (I think) by simply removing the escaping on the slash characters.

~(\r)\n

Comments