我有这个字符串:"@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
答案 0 :(得分:1)
做你所描述的事情:
subj.gsub(/_([^_;]*);/, '\1')
如果您只想删除"_"
和";"
,请执行以下操作:
subj.tr("_;", "")