如何在Ruby中改变……(elipses)到……(三个时期)?

我正在使用nokogiri解析这个文档 。 我发现该页面中有一些 (elipses)字符,无法删除。 我想知道如何使用Ruby将所有 (elipses)替换为... (三个句点)。

顺便说一句,你可以搜索这个字符串来查找所有…s

指定是否ALTER TABLE

编辑:我添加了我的程序和错误消息。

 # encoding: UTF-8 require 'nokogiri' require 'open-uri' require 'terminal-table' def change s {Nokogiri::HTML(" ").text => " ", Nokogiri::HTML(""").text => '"', Nokogiri::HTML("™").text => '(TM)', Nokogiri::HTML("&").text => "&", Nokogiri::HTML("<").text => " ">", Nokogiri::HTML("©").text => "(C)", Nokogiri::HTML("®").text => "(R)", Nokogiri::HTML("¥").text => " "}.each do |k, v| s.gsub!(k, v) end s end doc = Nokogiri::HTML(open('http://msdn.microsoft.com/en-us/library/ms189782.aspx').read.tr("…","...")) temp = [] doc.xpath('//div[@class="tableSection"]/table[position() = 1]/tr').each do |e| temp << e.css("td, th").map(&:text).map(&:strip).map {|x| x = change x; x.split(/\n/).map {|z| z.gsub(/.{80}/mi, "\\0\n")}.join("\n")} end table = Terminal::Table.new table.headings = temp.shift table.rows = temp puts table 

错误:

 F:\dropbox\Dropbox\temp>ruby nokogiri.rb nokogiri.rb:21: invalid multibyte char (UTF-8) nokogiri.rb:21: invalid multibyte char (UTF-8) nokogiri.rb:21: syntax error, unexpected $end, expecting ')' ...ary/ms189782.aspx').read.tr("í¡","...")) ... ^ F:\dropbox\Dropbox\temp> 

它可能取决于您正在使用的文件的编码,但请尝试使用

 "\u2026" 

对于单字符3点又称“ 水平省略号 ”(您要替换的那个)。

 "It was a dark and stormy night…".gsub("…", "...") 

我首先在它上面使用String#tr。

 open("http://msdn.microsoft.com/en-us/library/ms189782.aspx").read.tr("…","...") 

然后运行Nokogiri ……