0

我有这个字符串:"@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
4

1 回答 1

1

做你描述的事情:

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

如果您只想删除"_"and ";",那么:

subj.tr("_;", "")
于 2013-07-16T15:08:03.363 回答