{"id":22,"date":"2008-12-25T22:46:57","date_gmt":"2008-12-26T02:46:57","guid":{"rendered":"http:\/\/adamrosenfield.com\/blog\/?p=22"},"modified":"2008-12-25T22:46:57","modified_gmt":"2008-12-26T02:46:57","slug":"not-your-average-regex-find-and-replace","status":"publish","type":"post","link":"http:\/\/adamrosenfield.com\/blog\/2008\/12\/25\/not-your-average-regex-find-and-replace\/","title":{"rendered":"Not your average regex find-and-replace"},"content":{"rendered":"<p>I was reading through some of the <a href=\"http:\/\/www.codinghorror.com\">Coding Horror<\/a> archives, and came across <a href=\"http:\/\/www.codinghorror.com\/blog\/archives\/000633.html\">this post<\/a> examining Microsoft Visual Studio&#8217;s use of regular expressions in its find-and-replace dialog.  Visual Studio has support for all of the usual regex constructs but throws in a lot of its own quirky constructs too.  I suppose they could be useful if you learned to use them.<\/p>\n<p>But this post isn&#8217;t about Visual Studio.  Jeff&#8217;s post contained an intriguing link to <a href=\"http:\/\/steve-yegge.blogspot.com\/2006\/06\/shiny-and-new-emacs-22.html\">Steve Yegge&#8217;s blog<\/a>, where Steve goes over some of the new features in Emacs 22.  And despite my being a long-time emacs user, I was pleasantly surprised to learn that <b>you can use elisp with find-and-replace<\/b>.  <\/p>\n<p>Now at first, this may not seem like a big deal.  You can do a lot with a plain vanilla find-and-replace (<code>M-x replace-string<\/code> and <code>M-x query-replace<\/code> (also bound to <code>M-%<\/code>)), and regex find-and-replaces (<code>M-x replace-regexp<\/code> and <code>M-x query-replace-regexp<\/code> (also bound to <code>C-M-%<\/code>)) are damn powerful.  But sometimes, when a sed script won&#8217;t do, an elisp find-and-replace can save you a lot of pain.<\/p>\n<p>To do an elisp find-and-replace, do a regular <code>replace-regexp<\/code>, and set the replacement expression to <code>\\,<\/code> (backslash-comma) followed by an elisp expression.  You can use backreferences <code>\\1<\/code> through <code>\\9<\/code> as string variables, and you can use <code>\\#<\/code> as an integer variable that is the total number of replacements made so far.<\/p>\n<p>Steve has <a href=\"http:\/\/steve-yegge.blogspot.com\/2006\/06\/shiny-and-new-emacs-22.html\">a number of great examples<\/a> which I&#8217;m not going to repeat here, so I encourage you to go over there and eat &#8217;em up.<\/p>\n<p>I was eager to try them out, and seeing as how Emacs 22 is over <a href=\"http:\/\/en.wikipedia.org\/wiki\/Emacs#Release_history\">two and half years old<\/a>, I figured my current Emacs install would be up-to-date.  Not so &#8211; my MacBook was still running Emacs 21.2.1, which came installed when I bought it in 2006.  Fortunately, with <a href=\"http:\/\/www.macports.org\/\">MacPorts<\/a> installed (<b>HIGHLY<\/b> recommended for anyone running OS X), upgrading was <em>almost<\/em> as easy as running<\/p>\n<p><code>sudo port install emacs<\/code><\/p>\n<p>I did <code>install<\/code> instead of <code>upgrade<\/code> because my emacs install was the default one that came installed and I hadn&#8217;t migrated it into MacPorts yet.  I said it was <em>almost<\/em> that easy because after it installed and I went to run it, the old Emacs 21 ran instead, even though the MacPorts binaries directory <code>\/opt\/local\/bin<\/code> precedes <code>\/usr\/bin<\/code> in my $PATH.  It turns out that bash uses a <a href=\"http:\/\/www.cyberciti.biz\/tips\/how-linux-or-unix-understand-which-program-to-run-part-i.html#hashtables\">hash table<\/a>, of which I was previously unaware, to cache where frequently used commands are located to avoid searching your $PATH frequently.  To fix this, just run <code>hash -d emacs<\/code> to delete its binding for emacs (or <code>hash -r<\/code> to clear the entire table), and you&#8217;re good to go!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was reading through some of the Coding Horror archives, and came across this post examining Microsoft Visual Studio&#8217;s use of regular expressions in its find-and-replace dialog. Visual Studio has support for all of the usual regex constructs but throws in a lot of its own quirky constructs too. I suppose they could be useful [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-tools"],"_links":{"self":[{"href":"http:\/\/adamrosenfield.com\/blog\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/adamrosenfield.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/adamrosenfield.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/adamrosenfield.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/adamrosenfield.com\/blog\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":0,"href":"http:\/\/adamrosenfield.com\/blog\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"wp:attachment":[{"href":"http:\/\/adamrosenfield.com\/blog\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/adamrosenfield.com\/blog\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/adamrosenfield.com\/blog\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}