-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Description
Hi,
I think this package is greatly underappreciated. Here's an example of using it to build regexps matching URLs, getting the parts of it into groups, and testing whether HTTP or HTTPS (or either) are used:
(define-arx url-rx
'((http (seq bos (group "http") "://") )
(https (seq bos (group "https") "://") )
(https? (seq bos (group "http" (optional "s")) "://") )
(protocol (seq bos (group (1+ (not (any ":")))) "://"))
(host (group (1+ (not (any "/")))))
(path (group "/" (1+ (not (any "?")))))
(query (seq "?" (group (1+ (not (any "#"))))))
(fragment (seq "#" (group (1+ anything))))))
;; Accept HTTP or HTTPS
(let ((url "http://server/path?query#fragment"))
(when (string-match (url-rx https? host path (optional query) (optional fragment)) url)
(list (match-string 0 url)
(match-string 1 url)
(match-string 2 url)
(match-string 3 url)
(match-string 4 url)
(match-string 5 url)))) ;=> ("http://server/path?query#fragment" "http" "server" "/path" "query" "fragment")
;; Only accept HTTPS, not plain HTTP
(let ((url "http://server/path?query#fragment"))
(when (string-match (url-rx https host path (optional query) (optional fragment)) url)
(list (match-string 0 url)))) ;=> nil
;; Accept any protocol, not just HTTP
(let ((url "ftp://server/path"))
(when (string-match (url-rx protocol host path (optional query) (optional fragment)) url)
(list (match-string 0 url)
(match-string 1 url)
(match-string 2 url)
(match-string 3 url)
(match-string 4 url)
(match-string 5 url)))) ;=> ("ftp://server/path" "ftp" "server" "/path" nil nil)
It would be great if this were added to the readme. :)
In fact, this is common enough of a need that it would be great if there were an arx-url macro already defined by this package! :)
CeleritasCelery
Metadata
Metadata
Assignees
Labels
No labels