regexvimsubstitution

vim :s command with regex to select a second occurance of a character


I want this block of text below :-

119, 'Youth Campus,Janakpur', Janakpur  
978, 'Yog Kumar Janta Campus, Balwa-3', Mahottari  
806, 'Yerawati Aadarsha Multiple Campus, Lalmatiya Deukhuri, Dang', Dang  
613, 'Yasok Campus, Yasok, Panchther', Panchthar  
458, 'Yagyodaya Dudhnath Tharu Multiple Campus,Suddhodhan-3,Ramawapur', Rupandehi  
1008, 'Yagnyabhumi Multiple Campus, Dharapani', Dhanusha  
546, 'Xavier International College, Kamalpokhari,kath', Kathmandu  
362, 'Xavier Academy Science College, Lazimpat, Kathmandu', Kathmandu  

to change to :-

119, 'Youth Campus', Janakpur  
978, 'Yog Kumar Janta Campus', Mahottari  
806, 'Yerawati Aadarsha Multiple Campus', Dang  
613, 'Yasok Campus', Panchthar  
458, 'Yagyodaya Dudhnath Tharu Multiple Campus', Rupandehi  
1008, 'Yagnyabhumi Multiple Campus', Dhanusha  
546, 'Xavier International College', Kathmandu  
362, 'Xavier Academy Science College', Kathmandu  

by using vim :s command. For that I need to delete the characters after the second , upto a ' character to it's right. How do I specify this condition using regex?

The :s/,.*',/,/g deletes the entire text inside the 's. and :%s/,.*'/','/g also doesn't work as expected.

I need a substitute command that gets the result along with some explanation of how the regex works/functions!


Solution

  • I suggest

    :%s/\v^(\d+,\s*'[^',]*).*(',[^,]*)$/\1\2/g
    

    See the PCRE-equivalent demo of this regex.

    Details