0

I have this string: "@OKFN @417_tw The _European Commission; && _European Governments; are consulting on Open Research Data; read our response: http://blog.okfn.org/2013/07/16/ec-consultation-on-open-research-data" and i want to replace all couples of words preceded by "_" and ended with ";" by the same words but without those characters.

Here's how i did it, but i dont think its very neat. Would there be a better way to do it?

subj="@OKFN @417_tw The _European Commission; && _European Governments; are consulting on Open Research Data; read our response: http://blog.okfn.org/2013/07/16/ec-consultation-on-open-research-data"

tags=subj.scan(/_[a-zA-Z0-9]+\s*[a-zA-Z0-9]+;/)

tags.each do |t|
    t.gsub!(/_|;/,"")
    subj.sub!("_"+t+";",t)

end

puts subj
3
  • i want to remove "_" and ";" but not in all ocurrencies Commented Jul 16, 2013 at 15:11
  • but not in all ocurrencies means ? give us a sample short string and the expected output from that string. Myself at-least not getting the point of yours. Commented Jul 16, 2013 at 15:11
  • not in 417_tw for instance, just when its 2 words, the first one preceded by "_" and the last one ended in ";" Commented Jul 16, 2013 at 15:14

1 Answer 1

1

To do what you describe:

subj.gsub(/_([^_;]*);/, '\1')

If you just want to delete "_" and ";", then this:

subj.tr("_;", "")
Sign up to request clarification or add additional context in comments.

1 Comment

this is removing "_" from the twitter user @417_tw

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.