| Bytes | Lang | Time | Link |
|---|---|---|---|
| 181 | Java | 250929T090021Z | Mark |
| 040 | JavaScript | 180214T184140Z | Shaggy |
| 123 | C | 150709T175654Z | openaddr |
| 024 | K | 150709T110227Z | tmartin |
| 022 | CJam | 150605T021421Z | Dennis |
| 038 | Gema | 150708T154411Z | manatwor |
| 190 | Javascript | 150604T223544Z | SuperJed |
| 064 | JavaScript ES6 | 150605T103832Z | edc65 |
| 097 | Rebol | 150605T155613Z | draegtun |
| 018 | Pyth | 150605T012845Z | Maltysen |
| 4336 | PHP 4.1 | 150605T120641Z | Ismael M |
| 025 | Perl | 150605T143105Z | Dennis |
| 087 | Python | 150605T141413Z | shaunakd |
| 037 | Bash | 150605T123356Z | Toby Spe |
| 053 | Bash | 150605T012533Z | Daniel W |
| 025 | Pyth | 150605T002148Z | Ypnypn |
| 027 | Retina | 150604T224550Z | Martin E |
Java, 181
String p(String k){String[]ps=url.split("\\?")[1].split("&");for(String p:ps){String[]kv=p.split("=",2);if(kv[0].equals(k))return kv.length>1?kv[1].replace("%20"," "):"";}return"";}
String p(String k) {
String[] ps = url.split("\\?")[1].split("&");
for (String p : ps) {
String[] kv = p.split("=", 2);
if (kv[0].equals(k))
return kv.length > 1 ? kv[1].replace("%20", " ") : "";
}
return "";
}
C, 123 bytes
char*f(char*s,char*n){for(char*x=n;*s&&(*x||*s!='=');x=*x++==*s++?x:n);return *s=='='?(n=strstr(s++,"&"))?(*n=0)?s:s:s:"";}
Note: this function assumes you aren't passing a constant char to it.
K, 24
{((!)."S=&"0:*|"?"\:x)y}
.
{((!)."S=&"0:*|"?"\:x)y}["https://example.com/index.php?genre=action&reaction=1&action=2";`genre]
"action"
Explanation:
The function takes two parameters, the url x and the key y
First we split the url on the ? character.
k)"?"\:"https://example.com/index.php?genre=action&reaction=1&action=2"
"https://example.com/index.php"
"genre=action&reaction=1&action=2"
Then we take the last element using *| (first reverse)
k)*|"?"\:"https://example.com/index.php?genre=action&reaction=1&action=2"
"genre=action&reaction=1&action=2"
Process the string using 0:. The LHS of 0: is a three character string, where the first is the datatype of the key (S for symbol, I for int etc), the second is the key-value delimiter (= in this case) and the third is the record delimiter (& here).
k)"S=&"0:*|"?"\:"https://example.com/index.php?genre=action&reaction=1&action=2"
genre reaction action
"action" ,"1" ,"2"
Turn this into a dictionary using !
k)(!)."S=&"0:*|"?"\:"https://example.com/index.php?genre=action&reaction=1&action=2"
genre | "action"
reaction| ,"1"
action | ,"2"
Then do a lookup on this dictionary for whichever key you're looking for.
k)((!)."S=&"0:*|"?"\:"https://example.com/index.php?genre=action&reaction=1&action=2")`genre
"action"
Turn it into a function using {} and replace the url string with x and the lookup key with y, then pass the url and key in as paramters (func[param1;param2;...;paramN])
k){((!)."S=&"0:*|"?"\:x)y}["https://example.com/index.php?genre=action&reaction=1&action=2";`genre]
"action"
CJam, 22 bytes
l'?'&er"&="l*/1>s'&/0=
Try it online in the CJam interpreter.
How it works
l e# Read a line from STDIN.
'?'&er e# Replace question marks with ampersands.
"&="l* e# Place the second line from STDIN between '&' and '='.
/ e# Split the first string at occurrences of the second.
1>s e# Discard the first chunk.
'&/0= e# Discard everything following an ampersand.
Gema: 44 38 characters
Expects first the parameter name then the URL on separate lines.
\B\L*\n=@set{p;$1}
\I$p\=/[^&]+/=$1
?=
Sample run:
bash-4.3$ gema -p '\B\L*\n=@set{p;$1};\I$p\=/[^&]+/=$1;?=' <<< $'action\nhttps://example.com/index.php?genre=action&reaction=1&action=2'
2
Javascript, 235 228 203 190 bytes
Takes the URL as the first input and the key as the second.
b=document.createElement('a'),b.href=prompt(u={}),a=b.search;if(a!=""){t=a.substring(1).split("&");for(i=t.length;i--;){p=t[i].split("=");u[p[0]]=p[1]}}c=u[prompt()];alert(c==undefined?"":c)
JavaScript (ES6) 64
Edit Saved 1 byte thx @ismael-miguel
A function with url and key as parameters.
Test running the snippet in Firefox.
f=(u,k,e='')=>u.replace(RegExp(`[?&]${k}=([^&]*)`),(m,g)=>e=g)&&e
// TEST
out=x=>O.innerHTML += x+'\n';
test=[
'https://example.com/index.php?action=delete',
'https://this.example.com/cat/index.php?action=delete',
'http://example.com',
'https://action.example.com/actionscript/action.jpg',
'https://example.com/index.php?genre=action&reaction=1&action=2'
]
test.forEach(u=>out('Url '+u+'\nValue ['+f(u,'action')+']'))
<pre id=O></pre>
Rebol - 97
e:["&"| end]p: func[u a][parse u[thru"?"any[copy k to"=""="copy v to e e(if k = a[return v])]]{}]
Ungolfed:
e: ["&" | end]
p: func [u a] [
parse u [
thru "?"
any [
copy k to "=" "=" copy v to e e
(if k = a [return v])
]
]
{}
]
Example usage (in Rebol console):
>> p "http://sub.example.com/some/dir/file.php?action=delete&file=test.foo&strict=true" "action"
== "delete"
>> p "https://example.com/index.php?action=delete" "action"
== "delete"
>> p "https://this.example.com/cat/index.php?action=delete" "action"
== "delete"
>> p "http://example.com, https://action.example.com/actionscript/action.jpg" "action"
== ""
>> p "https://action.example.com/actionscript/action.jpg" "action"
== ""
>> p "http://example.com" "action"
== ""
>> p "https://example.com/index.php?genre=action&reaction=1&action=2" "action"
== "2"
Pyth - 22 21 18 bytes
Uses the obvious method of constructing a dictionary and using Thanks to @FryAmTheEggman for the error handling trick.} to check existence.
#@.dcR\=cecw\?\&zq
Can probably be golfed a little more. Doing explanation now. Added explanation:
# Error handling loop
@ Implicitly print dict lookup
.d Dictionary constructor, takes list of pairs
cR\= Map split with "=" as the second arg
c \& Split by "&"
e Last in sequence of
c Split
w Second input
\? By "?"
q Quit loop
PHP 4.1, 4336 bytes
There's no mention to do not use built-in function, so, here is my go:
<?=parse_str(parse_url($U,6))?0:$$P;
This restricts the use of the keys U and P on your URL.
You can pass the data as you please (POST, GET, COOKIE, ...), being the key U the url and P the parameter.
Thank you, @manatwork for this shorter version!
Also, an alternative, without the previous restrictions:
<?=parse_str(parse_url($U,6),$A)?0:$A[$P];
Old versions:
<?parse_str(parse_url($U,6),$A);echo$A[$P];
If you are fine with not being able to use the keys U and P:
<?parse_str(parse_url($U,6));echo$$P;
Perl, 25 bytes
/[?&]$^I=([^&]*)/;$_=$1
This reads the URL from STDIN and the key as an argument to -i. It also requires the -p switch. I've added two bytes to the count to account for i and p.
Example
$ perl 2>&- -e'/[?&]$^I=([^&]*)/;$_=$1' -piaction <<< 'http://example.com/?action=delete'
delete
Python, 87 bytes
The script takes the first url as the first argument and the key as the second.
import re,sys;print re.search(r'[?|&]'+sys.argv[2]+'+=([a-z.]+)',sys.argv[1]).group(1);
Sample output:
>> runfile('test.py', args='http://sub.example.com/some/dir/file.php?action=delete&file=test.foo&strict=true file')
>> test.foo
Bash, 37
a=${2#*\?};eval "${a//&/;};echo \$$1"
Pass the key in the first argument and the URL in the second.
Strips everything off up to and including the first ?, then treats the remainder as a series of assignments (RFC 1738 URLs can't contain spaces or any of the shell metacharacters, so this works). Finally, print the requested result; by default, strings not found are assumed empty.
Limitation - any key that already exists in the environment may be 'found' when not present in the input.
Bash - 55 53
2 bytes saved thanks to izabera
[[ $1 =~ [?\&]$2=([^&]*) ]]
echo ${BASH_REMATCH[1]}
Takes the URL as the first arg and the key to find as the second.
bash url.sh "https://this.example.com/cat/index.php?action=delete&foo=bar&spam=eggs" spam
Pyth, 25 bytes
|smecd\=fqhcT\=zcecw\?\&"
Expects the input on two lines, the key followed by the URL.
Explanation
cw\? Split the second input (the URL) by "?"
e The end (the parameter list)
c \& Split the parameter list by "&"
f Filter each parameter T
cT\= Split by "="
h The first part (the key)
q z Equals the first input (the search key)
m Map each valid parameter d
cd\= Split d by "="
e The value
s Print as a string
| " Otherwise, if the key isn't found, print nothing
Retina, 30 27 bytes
!`(?<=^\1 .*[?&](.*)=)[^&]+
This is a full program that expects the key and the URL to be space-separated on STDIN, e.g.
action http://sub.example.com/some/dir/file.php?action=delete&file=test.foo&strict=true
This is essentially just a (.NET-flavoured) regex, which matches the correct value by checking that the preceding key is equal to the first string in the input (using a backreference). The only thing that's a bit odd about the regex is that .NET matches lookbehinds from right to left, which is why the "backreference" actually references a group that appears later in the pattern. The !` instructs Retina to print the actual match (if there is one) instead of the number of matches.
For those who don't regex, here is an annotated version. Read the lookbehind from the bottom up, because that's how it's processed:
(?<= # A lookbehind. Everything we match in here will not be returned as part of
# actual match. Start reading this from the corresponding parenthesis.
^ # Ensure we're at the beginning of the input, so that we've checked
# against the entire input key.
\1 # Backreference to the key to check that it's the one we've asked for.
[ ] # Match a space.
.* # Consume the rest of the URL.
[?&] # Match a ? or a & to ensure we've actually captured the entire key.
( # End of group 1.
.+ # Match the key.
) # Capturing group 1. Use this to keep track of the key.
= # Make sure we start right after a '='
) # Start reading the lookbehind here.
[^&]+ # Match the value, i.e. as many non-& characters as possible.