2

I want to replace space characters with plus signs in the search query I send to google.

(defn search-google [search-term]
  (clojure.string/replace search-term #" " "+")
    (browse-url 
      (str "http://google.ca/search?q=" search-term)))

However my function fails as my search-term retains spaces.

(search-google "clojure user input")

URISyntaxException Illegal character in query at index 32:
http://google.ca/search?q=clojure user input  java.net.URI$Parser.fail (:-1)

I am new at clojure and suspect I am missing something obvious.

3 Answers 3

3

A new fixed search-term is being created, then the fixed one is ignored and the unchanged one is passed to browse-url:

(def browse-url println)                                                                                                                                                  
(defn search-google [search-term]                                                                                                                                         
  (let [fixed-search-term (clojure.string/replace search-term #" " "+")]                                                                                                  
    (browse-url                                                                                                                                                           
     (str "http://google.ca/search?q=" fixed-search-term))))  

user> (search-google "a b c")                                                                                                                                             
http://google.ca/search?q=a+b+c                                                                                                                                           
nil    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Arthur and @andrewdotnich for the explanation about let.
2

Try this instead:

(defn search-google [search-term]
  (browse-url 
   (str "http://google.ca/search?q="
        (clojure.string/replace search-term #"\s+" "+"))))

Notice that the pattern is a regular expression, and you can use \s+ to indicate that the string match to be replaced should include one or more spaces.

Also be aware that the replace procedure returns a new string, the original string is not modified.

4 Comments

Sorry, that didn't work. Maybe I have to re-bind search-term to it's new value. In python it would be search_term = search_term.replace(' ','+')
@exbctel no need to do that, the string is correctly formed before being passed as a parameter, to see it try this: (str "http://google.ca/search?q=" (clojure.string/replace "a b c d" #"\s+" "+")) maybe there are other erroneous characters in the search term?
Thanks Oscar. Moving the string/replace into browse-url worked. However I ended up using the let re-binding suggested by Arthur and Andrew instead.
@exbctel you're welcome! remember that let is useful, but not required to make the code work - you can pass the value directly, as shown above. Also be aware that the other answers don't take in consideration the case where there are more than one spaces in-between
1

Replace doesn't modify search-term in-place, but rather returns a copy of it, which is being thrown away.

Use let to bind this value to a name in the function and call browse-url with that instead.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.