title: PHP regex to detect pagination
body: <p>I'm re-writting a route handling class for a MVC based site in PHP and need a regex to detect a pagination string in the URL. The pagination string is formed of three different parts;</p>
  
  <ul>
  <li><strong>Page number detection:</strong> <code>/page/[NUMERIC]/</code></li>
  <li><strong>Items per page detection:</strong> <code>/per_page/[NUMERIC]/</code></li>
  <li><strong>Ordering detection:</strong> <code>/sort/[ALMOST_ANY_CHARACTER]/[asc or desc]/</code></li>
  </ul>
  
  <p>Due to the way it was previously developed, these three parts can be in any order. There are a number of existing links which I need to keep working plus the code used to handle pagination (no plans for a re-write yet) - so changing the pagination code to always generate a consistent url isn't possible.</p>
  
  <p>Therefore, I need to build a regex pattern to detect every possible combination of the pagination structure. I have three patterns to detect each part, which are as follows:</p>
  
  <ul>
  <li><strong>Page number detection:</strong> <code>(page/\d+)</code></li>
  <li><strong>Items per page detection:</strong> <code>(per_page/\d+)</code></li>
  <li><strong>Ordering detection:</strong> <code>(sort/([a-zA-Z0-9\.\-_%=]+)/(asc|desc))</code></li>
  </ul>
  
  <p>Being new to writing complex (well this is complex to me anyway!) regex patterns, the only I can think of doing it is two combine the three patterns I have for each of the url structures (eg /pagenum/ordering/perpage/, /pagenum/perpage/ordering/) and using the | operator as an 'or' statement.</p>
  
  <p>Is there a better / more efficient way of doing this?</p>
  
  <p>I am running the regex using <code>preg_match</code>.</p>