VIM - Replace based on a search regex

前端 未结 1 1990
悲哀的现实
悲哀的现实 2021-01-29 15:58

I\'ve got a file with several (1000+) records like :

lbc3.*\'

ssa2.*\'

lie1.*\'

sld0.*\'

ssdasd.*\'

I can find them all by :



        
相关标签:
1条回答
  • 2021-01-29 16:15

    In general, the solution is to use a capture. Put \(...\) around the part of the regex that matches what you want to keep, and use \1 to include whatever matched that part of the regex in the replacement string:

       s/\(s[w|l].*[0-9].*\)\.\*'$/\1\\.*'/
    

    Since you're really just inserting a backslash between two strings that you aren't changing, you could use a second set of parens and \2 for the second one:

       s/\(s[w|l].*[0-9].*\)\(\.\*'\)$/\1\\\2/
    

    Alternatively, you could use \zs and \ze to delimit just the part of the string you want to replace:

       s/s[w|l].*p0-9].*\zs\ze\*\'$/\\/
    
    0 讨论(0)
提交回复
热议问题