title: How to do a negative lookbehind within a %r<…>-delimited regexp in Ruby?
body: <p>I like the <code>%r&lt;…&gt;</code> delimiters because it makes it really easy to spot the beginning and end of the regex, and I don't have to escape any <code>/</code>. But it seems that they have an insurmountable limitation that other delimiters don't have?</p>
  
  <p>Every other delimiter imaginable works fine:</p>
  
  <pre><code>/(?&lt;!foo)/
  %r{(?&lt;!foo)}
  %r[(?&lt;!foo)]
  %r|(?&lt;!foo)|
  %r/(?&lt;!foo)/
  </code></pre>
  
  <p>But when I try to do this:</p>
  
  <pre><code>%r&lt;(?&lt;!foo)&gt;
  </code></pre>
  
  <p>it gives this syntax error:</p>
  
  <pre><code>unterminated regexp meets end of file
  </code></pre>
  
  <p>Okay, it probably doesn't like that it's not a balanced pair, but how do you escape it such that it <em>does</em> like it?</p>
  
  <p><strong>Does something need to be escaped?</strong></p>
  
  <p>According to <a href="https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#The_%25_Notation" rel="noreferrer">wikibooks.org</a>:</p>
  
  <blockquote>
    <p>Any single non-alpha-numeric character can be used as the delimiter,
    <code>%[including these], %?or these?, %~or even these things~</code>.
    By using this notation, the usual string delimiters " and ' can appear
    in the string unescaped, but of course the new delimiter you've chosen
    does need to be escaped.</p>
  </blockquote>
  
  <p>Indeed, escaping is needed in these examples:</p>
  
  <pre><code>%r!(?&lt;\!foo)!                                                             
  %r?(\?&lt;!foo)? 
  </code></pre>
  
  <p>But if that were the only problem, then I should be able to escape it like this and have it work:</p>
  
  <pre><code>%r&lt;(?\&lt;!foo)&gt;
  </code></pre>
  
  <p>But that yields this error:</p>
  
  <pre><code>undefined group option: /(?\&lt;!foo)/
  </code></pre>
  
  <p>So maybe escaping is <em>not</em> needed/allowed? <a href="https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#The_%25_Notation" rel="noreferrer">wikibooks.org</a> does list <code>%&lt;pointy brackets&gt;</code> as one of the exceptions:</p>
  
  <blockquote>
    <p>However, if you use
    <code>%(parentheses), %[square brackets], %{curly brackets}</code> or
    <code>%&lt;pointy brackets&gt;</code> as delimiters then those same delimiters
    can appear <em>unescaped</em> in the string as long as they are in <em>balanced</em>
    pairs</p>
  </blockquote>
  
  <p><strong>Is it a problem with <em>balanced</em> pairs?</strong></p>
  
  <p>Balanced pairs are no problem as long as you are doing something in the Regexp that requires them, like...</p>
  
  <pre><code>%r{(?&lt;!foo{1})}   # repetition quantifier
  %r[(?&lt;![foo])]    # character class
  %r&lt;(?&lt;name&gt;foo)&gt;  # named capture group
  </code></pre>
  
  <p>But what if you need to insert a left-side delimiter ({, [, or &lt;) inside the regex? Just escape it, right? Ruby seems to have no problem with escaped unbalanced delimiters <em>most</em> of the time...</p>
  
  <pre><code>%r{(?&lt;!foo\{)}                                                                  
  %r[(?&lt;!\[foo)]
  %r&lt;\&lt;foo&gt;
  </code></pre>
  
  <p>It's just when you try to do it in the middle of the "group options" (which I guess is what the <code>&lt;!</code> characters are classified as here) following a <code>(?</code> that it doesn't like it:</p>
  
  <pre><code>%r&lt;(?\&lt;!foo)&gt;
  # undefined group option: /(?\&lt;!foo)/
  </code></pre>
  
  <p>So how do you do that then and make Ruby happy? (without changing the delimiters)</p>
  
  <p><strong>Conclusion</strong></p>
  
  <p>The workaround is easy. I'll just change this particular regex to just use something else instead like <code>%r{…}</code> instead.</p>
  
  <p>But the questions remain...</p>
  
  <ol>
  <li>Is there really no way to escape the <code>&lt;</code> here?</li>
  <li>Are there really some regular expression that are simply impossible to write using certain delimiters like <code>%r&lt;…&gt;</code>?</li>
  <li>Is <code>%r&lt;…&gt;</code> the only regular expression delimiter pair that has this problem (where some regular expressions are impossible to write when using it). If you know of a similar example with <code>%r{…}</code>/<code>%r[…]</code>, do share!</li>
  </ol>
  
  <p><strong>Version info</strong></p>
  
  <p>Not that it probably matters since this syntax probably hasn't changed, but I'm using:</p>
  
  <pre><code>⟫ ruby -v
  ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]
  </code></pre>
  
  <p>Reference:</p>
  
  <ul>
  <li><a href="https://ruby-doc.org/core-2.6.3/Regexp.html" rel="noreferrer">https://ruby-doc.org/core-2.6.3/Regexp.html</a></li>
  <li><a href="https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#The_%25_Notation" rel="noreferrer">% Notation</a></li>
  </ul>