title: IIS URL rewrite regular expression define required string block with list of options
body: <p>I'm trying to build a regular expression that matches the following:</p>
  <p>This is the default English website URL: <a href="https://www.example.com/buy/apartment" rel="nofollow noreferrer">https://www.example.com/buy/apartment</a></p>
  <p>This is the default German website URL: <a href="https://www.example.com/de/kauf/appartement" rel="nofollow noreferrer">https://www.example.com/de/kauf/appartement</a></p>
  <p>The expression I now have: <code>^([a-z]{0,2})\/(buy|kauf)\/([a-zA-Z0-9-+]+)$</code></p>
  <p>So, breakdown of what I tried:</p>
  <ul>
  <li>Have an optional block for the language: <code>([a-z]{0,2})</code> to be used a parameter value of <code>language</code></li>
  <li>Escape the required forward slash <code>\/</code></li>
  <li>Required block that can have only have either value &quot;buy&quot; OR &quot;kauf&quot;: <code>(buy|kauf)</code>, no need to use this value as a parameter, so not sure if I even have to use parentheses</li>
  <li>Escape the required forward slash <code>\/</code></li>
  <li>Required block to capture the home type: <code>([a-zA-Z0-9-+]+)</code> to be used a parameter value for <code>type</code></li>
  </ul>
  <p>My IIS rewrite rule:</p>
  <pre><code>&lt;rule name=&quot;test rewrite&quot;&gt;
    &lt;match url=&quot;^([a-z]{0,2})\/(buy|kauf)\/([a-zA-Z0-9-+]+)$&quot; /&gt;
    &lt;action type=&quot;Rewrite&quot; url=&quot;homes.aspx?type={R:3}&amp;amp;language={R:1}&quot; /&gt;
  &lt;/rule&gt;
  </code></pre>
  <p>I've been trying to test it here: <a href="https://regex101.com/" rel="nofollow noreferrer">https://regex101.com/</a>
  But I'm not getting a match on either these URL, where I would want them both to match: <a href="https://www.example.com/buy/apartment" rel="nofollow noreferrer">https://www.example.com/buy/apartment</a> or <a href="https://www.example.com/de/kauf/appartement" rel="nofollow noreferrer">https://www.example.com/de/kauf/appartement</a></p>
  <p>URLs that should NOT match: <a href="https://www.example.com/en/all-objects" rel="nofollow noreferrer">https://www.example.com/en/all-objects</a> or <a href="https://www.example.com/contact" rel="nofollow noreferrer">https://www.example.com/contact</a></p>
  <p><strong>UPDATE 1</strong></p>
  <p>I'm now down to this expression: <code>https:\/\/(.*)?\/([a-z]{0,2})\/?(buy|kauf)\/([a-zA-Z0-9-+]+)$</code>
  See <a href="https://regex101.com/r/pieJWW/1" rel="nofollow noreferrer">https://regex101.com/r/pieJWW/1</a></p>
  <p>However, I seem to be unable to get the language parameter &quot;de&quot; in the 2nd URL as a group.</p>
  <p><strong>UPDATE 2</strong>
  In IIS web.config I don't use the part <code>https://www.example.com/</code> to match on, but rather the part after that. So based on @Ryszard Czech's answer I now have this, but I can't grab the lang parameter, neither with <code>{R:1}</code> or <code>{R:2}</code></p>
  <pre><code>    &lt;rule name=&quot;&quot;test rewrite&quot;&gt;
        &lt;match url=&quot;^(([a-z]{0,2})\/)?(buy|kauf)\/([^\/]+)$&quot; /&gt;
        &lt;action type=&quot;Rewrite&quot; url=&quot;homes.aspx?type={R:4}&amp;amp;language={R:1}&quot; /&gt;
      &lt;/rule&gt;
  </code></pre>