Match Variable Count of Repeating Words
<p>(I am stepping through this laboriously to get my thoughts in order. Skip to the end for the question!)</p>

<p>My input (file names) is like this</p>

<pre><code>word1.word2.word3.e01S01.word4.word5.ext
word1.word2.E03s04.word3.ext
word1.e24s02.ext
</code></pre>

<p>(Any number of words can be before or after ExxSxx; xx is 00-99, E and S are constants but may be uppercase or lowercase; there's always ext but it's variable alpha.)</p>

<p>My desired output for these examples:</p>

<pre><code>Word1 Word2 Word3 E01S01.ext
Word1 Word2 E03S04.ext
Word1 E24S02.ext
</code></pre>

<p>(All words before and including ExxSxx are kept; . is replaced with space; words are in title case; E and S are capitalised; words between ExxSxx and .ext are discarded; ext is appended unchanged.)</p>

<p>I can match a <em>known</em> number of repeats (here 2) between . with this regex (match 1 or more instances of not . up to a dot):</p>

<pre><code>[^\.]+\.
</code></pre>

<p>and I can group like this to be able to refer to what I want to extract:</p>

<pre><code>^([^\.]+)\.                so \u$1 gives Word1
</code></pre>

<p>I can repeat 2 times:</p>

<pre><code>^([^\.]+)\.([^\.]+)\.      so \u$1 \u$2 gives Word1 Word2
</code></pre>

<p>I can continue:</p>

<pre><code>^([^\.]+)\.([^\.]+)\.([eE][0-9]{2}[sS][0-9]{2})
                          so \u$1 \u$2 \U$3 gives Word1 Word2 ExxSxx
</code></pre>

<p>Then I skip over the remainder and grab .ext:</p>

<pre><code>^([^\.]+)\.([^\.]+)\.([eE][0-9]{2}[sS][0-9]{2}).*(\.[a-z]{3}$)
                          so \u$1 \u$2 \U$3\L$4 gives Word1 Word2 ExxSxx.ext
</code></pre>

<p>Here, finally, is my question:</p>

<p>Without explicitly repeating the pattern for the first x words can I get what I want with any number of .-separated words?  Would it be better to grab everything up to and including the known ExxSxx together with .ext and deal with capitalisation afterwards?</p>