Title: There's nothing regular about regular expressions
Date: June 10, 2006
Timestamp: 1149928080
Author: sjs
Tags: technology, book, regex
----


  <a href="http://web.archive.org/web/20061018215138/http://sami.samhuri.net/admin/content/edit/425" class="admintools" id="admin_article" style="display: none;">edit</a>
  <h2>There's nothing regular about regular expressions</h2>
  <p class="auth"><!-- Posted by <a href="mailto:sjs@uvic.ca">Sami Jensen Samhuri</a> -->
  <span class="typo_date" title="Sat, 10 Jun 2006 08:28:00 GMT">on Saturday, June 10, 2006</span></p>
  <p>I’m almost half way reading Jeffrey Friedl’s book <a href="http://web.archive.org/web/20061018215138/http://www.oreilly.com/catalog/regex2/">Mastering Regular Expressions</a> and I have to say that for a book on something that could potentially bore you to tears, he really does an excellent job of keeping it interesting. Even though a lot of the examples are contrived (I’m sure out of necessity), he also uses real examples of regexes that he’s actually used at <a href="http://web.archive.org/web/20061018215138/http://www.yahoo.com/">Yahoo!</a>.</p><p>As someone who has to know how everything works it’s also an excellent lesson in patience, as he frequently says “here, take this knowledge and just accept it for now until I can explain why in the next chapter (or in 3 chapters!)”. But it’s all with good reason and when he does explain he does it well.</p>


	<p>Reading about the different <span class="caps">NFA</span> and <span class="caps">DFA</span> engines and which tools use which made me go “ahhh, /that’s/ why I can’t do that in grep!” It’s not just that I like to know how things work either, he’s 100% correct about having to know information like that to wield the power of regexes in all situations. This book made me realize that regex implementations can be wildly different and that you really need to consider the job before jumping into using a specific regex flavour, as he calls them. I’m fascinated by learning why <span class="caps">DFA</span> regex implementations would successfully allow <tt>^\w+=.<strong>(\\\n.</strong>)*</tt> to match certain lines, allowing for trailing backslashes to mean continuation but why <span class="caps">NFA</span> engines would fail to do the same without tweaking it a bit.</p>


	<p>It requires more thinking than the last 2 computer books I read, “Programming Ruby”: (the “pixaxe” book) and <a href="http://web.archive.org/web/20061018215138/http://sami.samhuri.net/articles/2006/06/10/,">Agile Web Development With Rails</a> so it’s noticeably slower reading. It’s also the kind of book I will read more than once, for sure. There’s just no way I can glean everything from it in one reading. If you use regular expressions at all then you need this book. This is starting to sound like an advertisement so I’ll say no more.</p>


<span class="caps">QOTD</span>, p. 329, about matching nested pairs of parens:
<pre>\(([^()]|\(([^()]|\(([^()]|\(([^()])*\))*\))*\))*\)
Wow, that's ugly.
</pre>

	<p>(Don’t worry, there’s a much better solution on the next 2 pages after that quote.)</p>

