1

I am trying to construct a regexp for URLs (for grabbing them out quickly out of a paragraph, region, etc.). I don't need it advanced, but I am having some troubles with it. Here is my attempt, which doesn't work too well:

(re-search-forward (rx (minimal-match (and "http" (*? anything) (or ")" "\"" "[:blank:]")))))

This other question has some url regexps, but those aren't for emacs.

P.S. I also don't understand how match-beginning works as its description is a little bit mystic.

9
  • 1
    gnus-art.el defines gnus-button-url-regexp, rcirc.el defines rcirc-url-regexp, erc-button.el defines erc-button-url-regexp, ffap.el defines ffap-url-regexp, goto-addr.el defines goto-address-url-regexp, and that's just some of the built-in ones, so I recommend you reuse one of them. See also thingatpt.el. For match-beginning, see (eintr) fwd-para while, and (elisp) Match Data along with its subnodes.
    – Basil
    May 27, 2019 at 12:26
  • 2
    "I am having some troubles with it. Here is my attempt, which doesn't work too well" -- you should explain what "troubles" and "doesn't work" actually means, rather than expecting people to guess.
    – phils
    May 27, 2019 at 12:36
  • @Basil AFAICS those find just the beginning of a URL. So, if a link is inside parentheses, the parenthesis will be grabbed. That's what I tried to avoid in my regexp, but it is too greedy (try https://site.com ) ", it stops at the second space) May 27, 2019 at 12:38
  • 1
    @user3496846 Some Emacs variables only match the beginning, yes, but rcirc-url-regexp for one matches the entire URL. You can also piece together the parts you're interested in if you look at thingatpt.el.
    – Basil
    May 27, 2019 at 12:46
  • 1
    n.b. (rx "[:blank:]") matches the literal string [:blank:], not the character class. That's how double-quoted strings work in rx (i.e. like using regexp-quote). You wanted to use the symbol blank there.
    – phils
    May 27, 2019 at 12:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.