Advertisement
xoru

ValidURL

Jun 1st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Inspiration: https://www.purebasic.fr/english/vie...p?f=12&t=44359
  2.  
  3. ; Validates URLS
  4. ; --------------
  5. ; Must include a scheme such as http:// or ftp://
  6. ; Support for port numbers and numeric IPs
  7. ; Returns #True or #False
  8. ; -----------------------------------------------
  9.  
  10. ; Store this string in a constant
  11. #URL_PATTERN = "^([a-z0-9]+://)(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"
  12.  
  13. ; A DLL Procedure, which returns an int (1 or 0)
  14. ProcedureDLL.i ValidURL(url.s)
  15.   ; False by default
  16.   Protected result = #False 
  17.   
  18.   ; Create a regular expression, #PB_Any means that the new handle will be returned instead of predetermined
  19.   Protected regex = CreateRegularExpression(#PB_Any, #URL_PATTERN)
  20.   If(regex) ; check to make sure the regex was successfully created
  21.     ; Match the URL and store the result as a 1 or 0
  22.     result = Bool(MatchRegularExpression(regex, url)) ; Bool() converts the zero or non-zero result of Match to 1 or 0 (true or false)
  23.     
  24.     ; Free the resources used by the regex, we're done
  25.     FreeRegularExpression(regex)
  26.   EndIf 
  27.   
  28.   ; Return the result
  29.   ProcedureReturn result
  30. EndProcedure
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement