Match everything before last section of url <p>I want to match everything in a URL up to the last section.</p> <p>So I want to match: <code>http://www.test.com/one/two/</code> in all of the following cases:</p> <pre><code>http://www.test.com/one/two/three http://www.test.com/one/two/three/ http://www.test.com/one/two/three/?foo=bar </code></pre> <p>I'm working in PHP and currently I have <code>/.+\/(?=[^\/]+\/?$)/</code> this matches everything except the last case but I can't seem to 'not match a forward slash unless it's followed by a question mark' which would seeming sort the problem?</p>