Fetching a string from clojure collections using regex

can you suggest me the shortest and easiest way to extract a substring from a sequence of strings? I get this collection from using a command line that accepts content from a specific web page, and here is what I get as a result:

("background-image:url('http://s3.mangareader.net/cover/gantz/gantz-r0.jpg')" "background-image:url('http://s3.mangareader.net/cover/deadman-wonderland/deadman-wonderland-r0.jpg')" "background-image:url('http://s3.mangareader.net/cover/12-prince/12-prince-r1.jpg')" ) 

I would like some help in extracting the URL from each line in a sequence. I tried something with a split function, but without success. Can anyone suggest a regex or some other approach to this problem?

thanks

+4
source share
2 answers

re-seq in resque!

 (map #(re-seq #"http.*jpg" %) d) (("http://s3.mangareader.net/cover/gantz/gantz-r0.jpg") ("http://s3.mangareader.net/cover/deadman-wonderland/deadman-wonderland-r0.jpg") ("http://s3.mangareader.net/cover/12-prince/12-prince-r1.jpg")) user> 

re-find even better:

 user> (map #(re-find #"http.*jpg" %) d) ("http://s3.mangareader.net/cover/gantz/gantz-r0.jpg" "http://s3.mangareader.net/cover/deadman-wonderland/deadman-wonderland-r0.jpg" "http://s3.mangareader.net/cover/12-prince/12-prince-r1.jpg") 

because it does not add an extra layer of seq.

+5
source

Will there be something simple, how does it work for you?

 (defn extract-url [s] (subs s (inc (.indexOf s "'")) (.lastIndexOf s "'"))) 

This function will return a string containing all characters between the first and last single quotes.

Assuming your string sequence is called ss , then:

 (map extract-url ss) ;=> ("http://s3.mangareader.net/cover/gantz/gantz-r0.jpg" ; "http://s3.mangareader.net/cover/deadman-wonderland/deadman-wonderland-r0.jpg" ; "http://s3.mangareader.net/cover/12-prince/12-prince-r1.jpg") 

This is definitely not a general solution, but it is suitable for the input you provided.

+2
source

Source: https://habr.com/ru/post/1411382/


All Articles