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

后端 未结 7 1446
情歌与酒
情歌与酒 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条回答
  •  Happy的楠姐
    2020-12-02 16:18

    You can do the following.

    :%s/quick\(.*\)lazy/slow\1energetic
    

    The trick is to use the parens to match the text between the two words. You can then reference this text in the substitution string by using \1. You can also use \2 for the second matched paren expression and so on. This allows you to replace multiple words without disturbing the text inbetween.

提交回复
热议问题