So, I have a function that sends a URL to bit.ly for shortening and returns the short URL, and a (replace-regexp-in-string)
, which is supposed to grab the URL out of a string to passes it to that function.
Funny thing is -- both of these work fine on their own, but when I combine them, I end up with a mess.
(defun crshd/shorten-url (url)
(let* ((bitly-url (format "https://api-ssl.bit.ly/v3/shorten?access_token=%s&format=%s&longUrl=%s"
crshd--bitly-api-key
"txt"
url))
(response-buffer (url-retrieve-synchronously bitly-url)))
(with-current-buffer response-buffer
(buffer-substring (+ url-http-end-of-headers 1) (- (point-max) 1)))))
(replace-regexp-in-string "http.?://[a-z0-9./?&%#-_=]+"
'crshd/shorten-url
"replace http://emacs.stackexchange.com in this string"
nil nil)
And I end up with "replace http://emachttp://bit.ly/2a1zTGW.stackexchange.com in this string"
At the same time, (crshd/shorten-url "http://emacs.stackexchange.com")
returns "http://bit.ly/2a1zTGW"
, as expected.
(defun crshd/foo-url (url)
"foo")
(replace-regexp-in-string "http.?://[a-z0-9./?&%#-_=]+"
'crshd/foo-url
"replace http://emacs.stackexchange.com in this string"
nil nil)
Also returns "replace foo in this string"
.
What am I missing here?
save-match-data
to keep the Match Data unchaged after callingcrshd/shorten-url
.#-_
in your character alternative? I haven't checked what that would be, but it looks like a mistake.