Can I substitute multiple items in a single regular expression in VIM or Perl?

后端 未结 7 1451
情歌与酒
情歌与酒 2020-12-02 15:57

Let\'s say I have string \"The quick brown fox jumps over the lazy dog\" can I change this to \"The slow brown fox jumps over the energetic dog\" with one regular expression

7条回答
  •  无人及你
    2020-12-02 16:22

    There's a neat way to do it in Ruby using gsub with a block:

    s = "The quick brown fox jumps over the lazy dog"
    subs = {'quick' => 'slow', 'lazy' => 'industrious'}
    s.gsub(/quick|lazy/) { |match| subs[match] }
    # => "The slow brown fox jumps over the industrious dog"
    

提交回复
热议问题