title: How to linkify internal text document cross-references?
body: <p>I'm making a web version of a collection of docs with plaintext like this:</p>
  <blockquote>
  <p>...as found in article 6, depending on...</p>
  </blockquote>
  <p>I'm writing code to add relative URL anchors (linkify):</p>
  <pre class="lang-markdown prettyprint-override"><code>...as found in &lt;a href=&quot;article_6&quot;&gt;article 6&lt;/a&gt;, depending on...
  </code></pre>
  <p>I'm open to any programming language, and currently have Ruby + regex code that handles this simple case:</p>
  <pre class="lang-rb prettyprint-override"><code>    with_single_article_links = html.gsub(/(article \d+)/i) do
        last_match = Regexp.last_match
        &quot;&lt;a href=\&quot;last_match.gsub(' ', '_')\&quot;&gt;#{last_match}&lt;/a&gt;&quot;
      end
  </code></pre>
  <p>But I'm looking for ideas on handling more complex cases like these, with multiple citations:</p>
  <ul>
  <li>...as found in <strong>article 6 or 7</strong>, depending on...</li>
  <li>...as found in <strong>article 6, 7 or 8</strong>, depending on...</li>
  <li>...as found in <strong>article 6, 7 or 8 bis</strong>, depending on...</li>
  </ul>
  <p>If I keep going with my current code, I'd probably have two levels of regexes: a first match for <code>article \d+</code>, and then a second check for one of these complex cases.</p>
  <p>But is there some other approach I could take? I'm open to any programming language and technique. This is basically a reality check for me that I'm using a decent method.</p>
  <p><strong>Update:</strong> Expanding the regex, this is working so far:</p>
  <pre><code>article (\d+)((, \d+)* or (\d+))?
  </code></pre>
  <p>Live view: <a href="https://regex101.com/r/WHtM5C/1" rel="nofollow noreferrer">https://regex101.com/r/WHtM5C/1</a></p>
  <p>The second group will just need some simple parsing of the comma-separated list.</p>