title: How to capture more than 10 things using Swift 5.7's RegexBuilder?
body: <p>Let's say I have a file that stores information about people, and one of the lines look like this:</p>
  <pre><code>Sweeper 30 1992-09-22 China/Beijing - 0 2020-07-07 Mary/Linda - Pizza/Lemon
  </code></pre>
  <p>From left to right, it's name, age, date of birth, country of birth, city of birth, number of children, date of marriage (optional), wife's name (optional), ex-wife's name (optional), favourite food, least favourite food.</p>
  <p>I want to get <em>all</em> the information from the line using the Swift 5.7 RegexBuilder module, I tried:</p>
  <pre><code>let regex = Regex {
      /([a-zA-Z ]+)/ // Name
      &quot; &quot;
      TryCapture { OneOrMore(.digit) } transform: { Int($0) } // Age
      &quot; &quot;
      Capture(.iso8601Date(timeZone: .gmt)) // Date of Birth
      &quot; &quot;
      /([a-zA-Z ]+)/ // Country of Birth
      &quot;/&quot;
      /([a-zA-Z ]+)/ // City of Birth
      &quot; - &quot;
      TryCapture { OneOrMore(.digit) } transform: { Int($0) } // Children Count
      Optionally {
          &quot; &quot;
          Capture(.iso8601Date(timeZone: .gmt)) // Date of Marriage
          Optionally {
              &quot; &quot;
              /([a-zA-Z ]+)/ // Wife
              Optionally {
                  &quot;/&quot;
                  /([a-zA-Z ]+)/ // Ex-wife
              }
          }
      }
      &quot; - &quot;
      /([a-zA-Z ]+)/ // Favourite food
      &quot;/&quot;
      /([a-zA-Z ]+)/ // Least Favourite Food
  }
  </code></pre>
  <p>However, Swift says that it is unable to type check this in reasonable time.</p>
  <p>I know the reason this happens is because <a href="https://developer.apple.com/documentation/regexbuilder/regexcomponentbuilder" rel="nofollow noreferrer"><code>RegexComponentBuilder</code></a> (the result builder for regex components) only has overloads for up to 10 &quot;C&quot;s or something like that (not too sure on the details):</p>
  <pre><code>static func buildPartialBlock&lt;W0, W1, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, R0, R1&gt;(
      accumulated: R0,
      next: R1) -&gt; Regex&lt;(Substring, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10)&gt; where R0 : RegexComponent, R1 : RegexComponent, R0.RegexOutput == (W0, C1, C2, C3), R1.RegexOutput == (W1, C4, C5, C6, C7, C8, C9, C10
  )
  </code></pre>
  <p>If I make all the <code>Optionally</code> parts <em>required</em>, the error message becomes a bit more apparent.</p>
  <blockquote>
  <p>Ambiguous use of 'buildPartialBlock(accumulated:next:)'</p>
  </blockquote>
  <p>SwiftUI has a similar problem, where the number of views in a view builder cannot exceed 10, in which case you just use a <code>Group</code> to make some of the views a <em>single</em> view. Can you do something similar in RegexBuilder? Make some of the captures a single capture? It seems to have something to do with <code>AnyRegexOutput</code>, but I'm not sure how to use it.</p>
  <p>How do I resolve this compiler error?</p>
  <hr>
  <p>To avoid an XY problem:</p>
  <p>I have a data file where the data is formatted very haphazardly, i.e. not very machine-readable at all like CSV or JSON. Lines are written in all sorts of formats. Random delimiters are used in random places.</p>
  <p>Then another line in the file would have the same information, but formatted in a different way.</p>
  <p>What I want to do is to convert this weirdly formatted file into a easy-to-work-with format, like CSV. I've decided to do this with the Swift 5.7 RegexBuilder API. I would find a line in the file, write a regex that match that line, convert all the lines of the file that match that regex to CSV, then rinse and repeat.</p>
  <p>Therefore, I would like to avoid using multiple regexes to parse a single line, as this would mean that I would be writing a <em>lot</em> more regexes.</p>
  <p>I'm not sure if a parser like ANTLR4 would solve my problem. Given how randomly the file is formatted, I would need to be changing the parser a <em>lot</em>, causing the files to be generated again and again. I don't think that will be as convenient as using RegexBuilder.</p>