fin = File.new("C:\\1.txt")
tmp = []
fin.each_line do |line|
if line =~ /include.*?xercesc.*?>/
line.sub!(/(.*)<.*\/(.*?)>(.*)/, '\1"\2"\3')
end
tmp << line
end
fin.close
# puts tmp.join()
fout = File.new("C:\\2.txt", "w")
fout.write(tmp.join())
fout.close
测试输入文件1.txt:
this is the first line
xy
测试输出文件2.txt:
this is the first line
"include abc_xercesc.h"
xy "include 123_xercesc.c" zd