ruby on rails - How can I identify and process all URLs in a text string? -
I would like to enumerate all the URLs in a text string, for example:
Text = "fasòls http://george.it sdafsda"
found for each URL, I want to apply a function method (...)
Am string
I am using a method like this now:
msg = "" if i (i = ~ URI :: regexp) in text.split Nil? Msg = = "" + I and msg + = "" + method (i) end and text = msg
this works, but it is slow for long string how do i Can I increase
I think "gsub" is your friend here:
Category UrlParser attr_accessor: text, url_counter ,: urls def initial (text) @text = parse (text) end personal def pars (text) @ counter = 0 @urls = [] text.gsub (% R {(\ A | \ S +) (http: // [^ \ s] +)}) do @hamles & lt; & Lt; $ 2 "# {$ 1} # {replace_url ($ 2)}" end-end DRP replace_url (url) @counter + = 1 "[# {@ counter}]" end and processed_rill = urppres.new ("a http: // x .com / url two ") parsed_url.text puts parsed_url.urls
If you really need extra extraction of strings for long periods of time, Create a C extension.
Comments
Post a Comment