regex - Replace all occurrences except the first in Ruby. The regular expression spans multiple lines -


i trying down last 3200 tweets in groups of 200(in multiple pages) using restclient gem.

in process, end adding following lines multiple times file:

</statuses> <?xml version="1.0" encoding="utf-8"?> <statuses type="array"> 

to right(as xml parsing goes toss), after downloading file, want replace occurrences of above string except first. trying following:

 tweets_page = restclient.get("#{get_statuses_url}&page=#{page_number}")       message = <<-msg </statuses> <?xml version="1.0" encoding="utf-8"?> <statuses type="array"> msg       unless page_number == 1        tweets_page.gsub!(message,"")      end 

what wrong in above? there better way same?

i believe faster download whole bunch @ once , split body of response message , add first entry. this, can't try out consider idea.

tweets_page = restclient.get("#{get_statuses_url}").body tweets = tweets_page.split(message) tweets_page = tweets[0]+message+tweets[1..-1] 

you break them in groups of 200 also

if want gsub on whole text use following

tweets_page = <<-msg first </statuses> <?xml version="1.0" encoding="utf-8"?> <statuses type="array"> second </statuses> <?xml version="1.0" encoding="utf-8"?> <statuses type="array"> rest msg  message = <<-msg </statuses> <?xml version="1.0" encoding="utf-8"?> <statuses type="array"> msg  new_str = tweets_page.gsub message |match|   if defined? @first     ""   else     @first = true     message   end end  p new_str 

gives

type=\"array\">\nrest\n" "first\n</statuses>\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<statuses type=\"array\">\nsecond\nrest\n" 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -