我有这个字符串:"@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"
我想替换所有以“_”开头并以“;”结尾的单词 用同样的词,但没有那些字符。
这就是我的做法,但我认为它不是很整洁。会有更好的方法吗?
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