Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>/(?&lt;=&amp;|\?)foo(=[^&amp;]*)?(&amp;|$)/ </code></pre> <p>Uses lookbehind and the last group to "anchor" the match, and allows a missing value. Change the <code>\?</code> to <code>^</code> if you've already stripped off the question mark from the query string.</p> <p>Regex is still not a substitute for a real parser of the query string, however.</p> <p><em>Update:</em> Test script: (run it at <a href="http://codepad.org/" rel="nofollow noreferrer">codepad.org</a>)</p> <pre><code>import re regex = r"(^|(?&lt;=&amp;))foo(=[^&amp;]*)?(&amp;|$)" cases = { "foo=123": "", "foo=123&amp;bar=456": "bar=456", "bar=456&amp;foo=123": "bar=456", "abc=789&amp;foo=123&amp;bar=456": "abc=789&amp;bar=456", "oopsfoo=123": "oopsfoo=123", "oopsfoo=123&amp;bar=456": "oopsfoo=123&amp;bar=456", "bar=456&amp;oopsfoo=123": "bar=456&amp;oopsfoo=123", "abc=789&amp;oopsfoo=123&amp;bar=456": "abc=789&amp;oopsfoo=123&amp;bar=456", "foo": "", "foo&amp;bar=456": "bar=456", "bar=456&amp;foo": "bar=456", "abc=789&amp;foo&amp;bar=456": "abc=789&amp;bar=456", "foo=": "", "foo=&amp;bar=456": "bar=456", "bar=456&amp;foo=": "bar=456", "abc=789&amp;foo=&amp;bar=456": "abc=789&amp;bar=456", } failures = 0 for input, expected in cases.items(): got = re.sub(regex, "", input) if got != expected: print "failed: input=%r expected=%r got=%r" % (input, expected, got) failures += 1 if not failures: print "Success" </code></pre> <p>It shows where my approach failed, Mark has the right of it&mdash;which should show why you shouldn't do this with regex.. :P</p> <hr> <p>The problem is associating the query parameter with exactly one ampersand, and&mdash;if you must use regex (if you haven't picked up on it :P, I'd use a separate parser, which might use regex inside it, but still actually understand the format)&mdash;one solution would be to make sure there's exactly one ampersand per parameter: replace the leading <code>?</code> with a <code>&amp;</code>.</p> <p>This gives <code>/&amp;foo(=[^&amp;]*)?(?=&amp;|$)/</code>, which is very straight forward and the best you're going to get. Remove the leading <code>&amp;</code> in the final result (or change it back into a <code>?</code>, etc.). Modifying the test case to do this uses the same cases as above, and changes the loop to:</p> <pre><code>failures = 0 for input, expected in cases.items(): input = "&amp;" + input got = re.sub(regex, "", input) if got[:1] == "&amp;": got = got[1:] if got != expected: print "failed: input=%r expected=%r got=%r" % (input, expected, got) failures += 1 if not failures: print "Success" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload