Page 1 of 1

URLComponents

Posted: Mon Jan 03, 2022 8:15 pm
by Emily-Elizabeth
This will take a URL and break it down to the different parts.

Code: Select all

function URLComponents pURL
   local tSchema, tAuthority, tPath, tQuery, tFragment, tTemp
   get matchText(pURL, "^(([^:\/?#]+):)?(\/?\/?([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$", tTemp, tSchema, tTemp, tAuthority, tPath, tTemp, tQuery, tTemp, tFragment)
   
   local tUserInfo, tHost, tPort
   get matchText(tAuthority, "(?:((?:[A-Za-z0-9\-._~!$&\'()*+,;=:]|%[0-9A-Fa-f]{2})*)@)?((?:[A-Za-z0-9\-._~!$&\'()*+,;=]|%[0-9A-Fa-f]{2})+)(?::([0-9]*))?", \
         tUserInfo, tHost, tPort)
   
   local tResults
   put tSchema into tResults["Schema"]
   put tUserInfo into tResults["UserInfo"]
   put tHost into tResults["Host"]
   put tPort into tResults["Port"]
   put tPath into tResults["Path"]
   put tQuery into tResults["Query"]
   put tFragment into tResults["Fragment"]
   
   return tResults
end URLComponents

Re: URLComponents

Posted: Mon Jan 03, 2022 8:42 pm
by Klaus
Hi Emily-Elizabeth,

welcome to the forum!

Thank you, and to what do we owe this honor? :D


Best

Klaus

P.S.
Personal note:
A little "Hello" or something would not have hurt for the very first posting.

Re: URLComponents

Posted: Thu Feb 03, 2022 12:25 am
by mwieder
Nice! That's some serious regex.
And I learned something new... I wasn't aware that you could reuse tTemp like that for the don't-care variables.

Re: URLComponents

Posted: Wed Dec 10, 2025 1:14 am
by Emily-Elizabeth
The tUserInfo wasn't being split in to the two components: username and password. This is a version to correct that

Code: Select all

function URLComponents pURL
   local tSchema, tAuthority, tPath, tQuery, tFragment, tTemp
   get matchText(pURL, "^(([^:\/?#]+):)?(\/?\/?([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$", tTemp, tSchema, tTemp, tAuthority, tPath, tTemp, tQuery, tTemp, tFragment)
   
   local tUserInfo, tHost, tPort
   get matchText(tAuthority, "(?:((?:[A-Za-z0-9\-._~!$&\'()*+,;=:]|%[0-9A-Fa-f]{2})*)@)?((?:[A-Za-z0-9\-._~!$&\'()*+,;=]|%[0-9A-Fa-f]{2})+)(?::([0-9]*))?", \
         tUserInfo, tHost, tPort)
   
   split tUserInfo by ":"
   
   local tResults
   put tSchema into tResults["Schema"]
   put tUserInfo[1] into tResults["UserName"]
   put tUserInfo[2] into tResults["UserPassword"]
   put tHost into tResults["Host"]
   put tPort into tResults["Port"]
   put tPath into tResults["Path"]
   put tQuery into tResults["Query"]
   put tFragment into tResults["Fragment"]
   
   return tResults
end URLComponents

Re: URLComponents

Posted: Wed Dec 10, 2025 3:02 am
by Emily-Elizabeth
For those of us who like to use objects, I made this in to a library that returns instances of the button control. This allows you to create an instance and pass it through multiple handlers/functions without having to call the RegEx multiple times or having to pass multiple parameters.

https://github.com/emily-elizabeth/URLComponents_LC/