TextMate - A search and replace example with pattern matching, group capturing, and replacement variables

As a quick note, you can do a search-and-replace operation in TextMate using regular expression patterns as follows. First, create a find pattern like this, putting the capture group in between parentheses:

<h2 id="toc_.*">(.*)</h2>

Then create a “replace” pattern like this, using $1 as the variable for the first capture group:

<h2>$1</h2>

On the TextMate search and replace UI you also need to select the “regular expression” checkbox.

In the case of this example I was deleting a bunch of “cruft” that was generated by MacDown when I converting some Markdown text to HTML, but of course you can use this technique for any sort of search/replace/pattern/regex problem in TextMate.