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!
I suggest
:%s/\v^(\d+,\s*'[^',]*).*(',[^,]*)$/\1\2/g
See the PCRE-equivalent demo of this regex.
Details
^
- start of a string (line in Vim)(\d+,\s*'[^',]*)
- Group 1: one or more digits, a comma, zero or more whitespaces, '
and then zero or more chars other than '
and ,
.*
- any zero or more chars as many as possible(',[^,]*)
- Group 2: ',
+ zero or more chars other than ,
as many as possible$
- end of a string (here, line).