Query String Splitter (Web URL) Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
TheDewd
Posts: 1398
Joined: 19 Dec 2013, 11:16
Location: USA

Query String Splitter (Web URL)

10 Dec 2019, 14:38

I have long URLs that contain multiple parameters, sometimes with no value.

Example: http://www.website.com/folder1/folder2/site.aspx?Param1=xxxxxxx&Param2=xxxxxxx&Param3=&Param4=&Param5=&Param6=xxxxxxx&Param7=xxxxxxx

I want to be able to split each parameter and receive its value.

Example:

Code: Select all

'Param1':	xxxxxxx
'Param2':	xxxxxxx
'Param3':	
'Param4':	
'Param5':	
'Param6':	xxxxxxx
'Param7':	xxxxxxx
I've found some websites that do this:
https://www.freeformatter.com/url-parser-query-string-splitter.html
https://www.freecodeformat.com/query-string-splitter.php
https://mrcoles.com/urlparse/

I'd like a solution in AutoHotkey. I tried using RegExMatch but had trouble with the blank parameters.

Any advice? Thanks!
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
User avatar
boiler
Posts: 5653
Joined: 21 Dec 2014, 02:44

Re: Query String Splitter (Web URL)

10 Dec 2019, 15:04

Code: Select all

Param := []
URL := "http://www.website.com/folder1/folder2/site.aspx?Param1=xxxxxxx&Param2=xxxxxxx&Param3=&Param4=&Param5=&Param6=xxxxxxx&Param7=xxxxxxx"
Start := 1
while (Found := RegExMatch(URL, "Param\d+=([^&]*)", Match, Start))
{
	Param.Push(Match1)
	Start := Found + StrLen(Match1) + 1
}
loop, % Param.Count() ; or .MaxIndex() if older AHK version
	TextOut .= "'Param" A_Index "': " Param[A_Index] "`n"
MsgBox, % TextOut
User avatar
TheDewd
Posts: 1398
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Query String Splitter (Web URL)

10 Dec 2019, 15:13

@boiler,

Thanks! That works for the example URL, however the actual URL does not use "Param" as the parameter names.

Example: http://www.website.com/folder1/folder2/site.aspx?ABC=xxxxxxx&DEF=xxxxxxx&GHI=&JKL=&MNO=&PQR=xxxxxxx&STU=xxxxxxx&VWX=

The parameter names could be anything, and the parameter could have a blank value at the end of the URL as well, like the example above.

I think this will work: ([^&?]*)=([^&]*) (adapted from your code). What do you think?
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
User avatar
boiler
Posts: 5653
Joined: 21 Dec 2014, 02:44

Re: Query String Splitter (Web URL)

10 Dec 2019, 15:27

That does seem to work, with [^&?]* being a stand-in for \w+ or maybe better [^=]+. You could probably successfully use any of them in place of what I did:

Code: Select all

Name := [], Value := []
URL := "http://www.website.com/folder1/folder2/site.aspx?ABC=xxxxxxx&DEF=xxxxxxx&GHI=&JKL=&MNO=&PQR=xxxxxxx&STU=xxxxxxx&VWX="
Start := 1
while (Found := RegExMatch(URL, "(\w+)=([^&]*)", Match, Start))
{
	Name.Push(Match1)
	Value.Push(Match2)
	Start := Found + StrLen(Match1) + StrLen(Match2) + 2
}
loop, % Name.Count() ; or .MaxIndex() if older AHK version
	TextOut .= "'" Name[A_Index] "': " Value[A_Index] "`n"
MsgBox, % TextOut
User avatar
TheDewd
Posts: 1398
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Query String Splitter (Web URL)

10 Dec 2019, 15:31

Perfect! Thank you very much!
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
teadrinker
Posts: 1738
Joined: 29 Mar 2015, 09:41
Contact:

Re: Query String Splitter (Web URL)  Topic is solved

10 Dec 2019, 15:59

Why not like this:

Code: Select all

URL := "http://www.website.com/folder1/folder2/site.aspx?ABC=xxxxxxx&DEF=xxxxxxx&GHI=&JKL=&MNO=&PQR=xxxxxxx&STU=xxxxxxx&VWX="
MsgBox, % RegExReplace(URL, "s).+?(\w+)=([^&]*)", "'$1': $2`n")
User avatar
TheDewd
Posts: 1398
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Query String Splitter (Web URL)

10 Dec 2019, 16:08

teadrinker wrote:
10 Dec 2019, 15:59
Why not like this:

Code: Select all

URL := "http://www.website.com/folder1/folder2/site.aspx?ABC=xxxxxxx&DEF=xxxxxxx&GHI=&JKL=&MNO=&PQR=xxxxxxx&STU=xxxxxxx&VWX="
MsgBox, % RegExReplace(URL, "s).+?(\w+)=([^&]*)", "'$1': $2`n")
Haha! Awesome! :D
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.

Return to “Ask For Help”

Who is online

Users browsing this forum: ah_sis90, bibiczon, Bing [Bot], boiler, capriccio, coffeered, dinamikler, Google [Bot], Jimay and 65 guests