Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: URL example #7

Open
alphapapa opened this issue Aug 28, 2018 · 0 comments
Open

Suggestion: URL example #7

alphapapa opened this issue Aug 28, 2018 · 0 comments

Comments

@alphapapa
Copy link

alphapapa commented Aug 28, 2018

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! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant