re = /^(\d+,\s*'[^',]*).*(',[^,\n]*)$/m
str = '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 '
subst = '\\1\\2'
result = str.gsub(re, subst)
# Print the result of the substitution
puts result
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html