title: Square Brackets in Python Regular Expressions (re.sub)
body: <h1>The Issue</h1>
  
  <p>I'm migrating wiki pages from the FlexWiki engine to the FOSwiki engine using Python regular expressions to handle the differences between the two engines' markup languages. </p>
  
  <p>The <a href="http://wiki.squirrel-lang.org/default.aspx/SquirrelWiki/FormattingRules.html" rel="nofollow">FlexWiki markup</a> and the <a href="http://foswiki.org/System/TextFormattingRules" rel="nofollow">FOSwiki markup</a>, for reference.</p>
  
  <p>Most of the conversion works very well, <em>except</em> when I try to convert the renamed links.
  Both wikis support renamed links in their markup.</p>
  
  <p>For example, Flexwiki uses:</p>
  
  <pre><code>"Link To Wikipedia":[http://www.wikipedia.org/]
  </code></pre>
  
  <p>FOSwiki uses:</p>
  
  <pre><code>[[http://www.wikipedia.org/][Link To Wikipedia]]
  </code></pre>
  
  <p>Both of which produce a rewritten hyperlink.</p>
  
  <p>I'm using the regular expression</p>
  
  <pre><code>renameLink = re.compile ("\"(?P&lt;linkText&gt;[^\"]+)\":\[(?P&lt;linkTarget&gt;[^\[\]]+)\]")
  </code></pre>
  
  <p>to parse out the link elements from the FlexWiki markup, which after running through something like</p>
  
  <pre><code>"Link Text":[LinkTarget]
  </code></pre>
  
  <p>is reliably producing groups</p>
  
  <pre><code>&lt;linkText&gt; = Link Text
  &lt;linkTarget = LinkTarget
  </code></pre>
  
  <p>My issue occurs when I try to use re.sub to insert the parsed content into the FOSwiki markup.</p>
  
  <p>My experience with regular expressions isn't anything to write home about, but I'm under the impression that, given the groups </p>
  
  <pre><code>&lt;linkText&gt; = Link text
  &lt;linkTarget = LinkTarget
  </code></pre>
  
  <p>a line like</p>
  
  <pre><code>line = renameLink.sub ( "[[\g&lt;linkTarget&gt;][\g&lt;linkText&gt;]]" , line )
  </code></pre>
  
  <p>should produce</p>
  
  <pre><code>[[LinkTarget][Link Text]]
  </code></pre>
  
  <p>However, in the output to the text files I'm getting</p>
  
  <pre><code>[[LinkTarget [[Link Text]]
  </code></pre>
  
  <p>which breaks the renamed links.</p>
  
  <p>After a little bit of fiddling I managed a workaround, where</p>
  
  <pre><code>line = renameLink.sub ( "[[\g&lt;linkTarget&gt;][ [\g&lt;linkText&gt;]]" , line )
  </code></pre>
  
  <p>produces</p>
  
  <pre><code>[[LinkTarget][ [[Link Text]]
  </code></pre>
  
  <p>which, when displayed in FOSwiki looks like</p>
  
  <pre><code>[[Link Text
  </code></pre>
  
  <p>Which WORKS, but isn't very pretty.</p>
  
  <p>There are probably thousands of instances of these renamed links in the pages I'm trying to convert, so fixing it by hand isn't any good.
  For the record I've run the script under Python 2.5.4 and Python 2.7.3, and gotten the same results.</p>
  
  <p>Am I missing something really obvious with the syntax? Or is there an easy workaround?</p>
  
  <h1>Solution</h1>
  
  <p>There wasn't anything wrong with the original expression.</p>
  
  <p>I started running through the other regex's in my script and commented out lines I thought might be overlapping with the renamed-link expression. That appears to have done the trick, and as a semi-permanent fix I've separated the link-focused expressions and the other expressions into separate scripts, which I run one after another.</p>
  
  <p>I guess them moral here is to double-check that you don't have overlapping expressions.</p>
  
  <h2>Attempted Solutions (Just see Solution above)</h2>
  
  <p>String addition</p>
  
  <pre><code>line = renameLink.sub ( "[[\g&lt;linkTarget&gt;]" + "[\g&lt;linkText&gt;]]" , line )
  </code></pre>
  
  <p>produces</p>
  
  <pre><code>[[linkTarget [[Link Text]]
  </code></pre>
  
  <p>It doesn't matter how you slice the concatenation, the result is the same.</p>
  
  <p>Escaping the square brackets, e.g.</p>
  
  <pre><code>line = renameLink.sub ( "\[\[\g&lt;linkTarget&gt;\]\[\g&lt;linkName&gt;\]\]" , line )
  </code></pre>
  
  <p>produces</p>
  
  <pre><code>\[ [[LinkTarget\]] [Link Text\]\]
  </code></pre>