title: Zend Framework 2: Zend\Mvc\Router\Http\Regex optional value
body: <p>I have a controller and action which take a customer name and then do a search on the database to match the customer. Some customers may have a middle name, and some don't.</p>
  
  <p>I have the following two routes for this:</p>
  
  <pre><code>        'full-name' =&gt; array(
              'type' =&gt; 'regex',
              'options' =&gt; array(
                  'regex' =&gt; '/(?&lt;title&gt;Mr|Miss|Mrs|Ms)/(?&lt;firstname&gt;[a-zA-Z0-9_-]+)/(?&lt;middlename&gt;[a-zA-Z0-9_-]+)/(?&lt;lastname&gt;[a-zA-Z0-9_-]+)',
                  'defaults' =&gt; array(
                      'controller' =&gt; 'Application\Controller\Customers',
                      'action'     =&gt; 'index',
                  ),
                  'spec'  =&gt; '/%firstname%/%middlename%/%lastname%'
              )
          ),
          'name' =&gt; array(
              'type' =&gt; 'regex',
              'options' =&gt; array(
                  'regex' =&gt; '/(?&lt;title&gt;Mr|Miss|Mrs|Ms)(?&lt;firstname&gt;[a-zA-Z0-9_-]+)/(?&lt;lastname&gt;[a-zA-Z0-9_-]+)',
                  'defaults' =&gt; array(
                          'controller' =&gt; 'Application\Controller\Customers',
                          'action'     =&gt; 'index',
                  ),
                  'spec'  =&gt; '/%firstname%/%lastname%'
              )
          ),
  </code></pre>
  
  <p>I was wondering whether there is a way to combine them both into one regex rule, but I couldn't find anything that would work for the middle name. I tried a few things like:</p>
  
  <pre><code>(?&lt;middlename&gt;[a-zA-Z0-9_-]?)
  (?&lt;middlename&gt;[a-zA-Z0-9_-]+|^$)
  </code></pre>
  
  <p>But then it complained that there wasn't a matched routed when I tried it without the middle name.
  Obviously, I can leave it as it is since it works, but it would be nice if it was possible to combine both routes into one.</p>
  
  <p>Does anyone know of a way to do this?</p>
  
  <p>EDIT:
  I originally simplified the code a bit compared to what I actually need, but this led to solutions that wouldn't answer my question about optional value in regex. I have changed the code to reflect my why I can't have segment route.</p>
  
  <p>For reference, this was my initial code:</p>
  
  <pre><code>        'full-name' =&gt; array(
              'type' =&gt; 'regex',
              'options' =&gt; array(
                  'regex' =&gt; '/Customer/(?&lt;firstname&gt;[a-zA-Z0-9_-]+)/(?&lt;middlename&gt;[a-zA-Z0-9_-]+)/(?&lt;lastname&gt;[a-zA-Z0-9_-]+)',
                  'defaults' =&gt; array(
                      'controller' =&gt; 'Application\Controller\Customers',
                      'action'     =&gt; 'index',
                  ),
                  'spec'  =&gt; '/%firstname%/%middlename%/%lastname%'
              )
          ),
          'name' =&gt; array(
              'type' =&gt; 'regex',
              'options' =&gt; array(
                  'regex' =&gt; '/Customer/(?&lt;firstname&gt;[a-zA-Z0-9_-]+)/(?&lt;lastname&gt;[a-zA-Z0-9_-]+)',
                  'defaults' =&gt; array(
                          'controller' =&gt; 'Application\Controller\Customers',
                          'action'     =&gt; 'index',
                  ),
                  'spec'  =&gt; '/%firstname%/%lastname%'
              )
          ),
  </code></pre>
  
  <p>Thanks!</p>