English 中文(简体)
Regex pop quiz of the day :D
原标题:Regex pop quiz of the day :D
  • 时间:2010-07-08 15:46:52
  •  标签:
  • ruby
  • regex

如果我这样说的话。

 There                   is             a lot           of           white space.

我也想去除鲁比斯群岛的所有不想要的空间。 你们如何确定白色空间并去除这一空间,以便在所有语种之间至少有一个白色空间?

到目前为止,我有:

gsub(/s{2,}/,   )

但是,正如你能够看到,这 words倒了几个词。

最佳回答

你结束发言。 在左边和右边穿透白色后,

str.strip.gsub(/s{2,}/,    )

用单一空间取代任何多空间。 当然,这假设你只处理实际空间。

问题回答

早在我撰写《佩勒法典》时,我就一直为我所操纵的操纵行为 gr污定期表达。 然后,一天,我想要制定一些正在搜索和修饰内容的法典,并写了一个基准,比较一些校外和标准校外搜查。 以指数为依据的搜索波及格外。 它既复杂又有时在处理简单问题时不需要老练。

不要立即 gr一个<代码>String.squeeze(>” ,而是能够更快地处理反复的空间压缩问题。 2. 审议基准的产出:

#!/usr/bin/env ruby

require  benchmark 

asdf =  There                   is             a lot           of           white space. 

asdf.squeeze(   ) # => "There is a lot of white space."
asdf.gsub(/  +/,    ) # => "There is a lot of white space."
asdf.gsub(/ {2,}/,    ) # => "There is a lot of white space."
asdf.gsub(/ss+/,    ) # => "There is a lot of white space."
asdf.gsub(/s{2,}/,    ) # => "There is a lot of white space."

n = 500000
Benchmark.bm(8) do |x|
  x.report( squeeze: ) { n.times{ asdf.squeeze(   ) } }
  x.report( gsub1: ) { n.times{ asdf.gsub(/  +/,    ) } }
  x.report( gsub2: ) { n.times{ asdf.gsub(/ {2,}/,    ) } }
  x.report( gsub3: ) { n.times{ asdf.gsub(/ss+/,    ) } }
  x.report( gsub4: ) { n.times{ asdf.gsub(/s{2,}/,    ) } }
end

puts
puts "long strings"
n     = 1000
str_x = 1000
Benchmark.bm(8) do |x|
  x.report( squeeze: ) { n.times{(asdf * str_x).squeeze(   ) }}
  x.report( gsub1: ) { n.times{(asdf * str_x).gsub(/  +/,    ) }}
  x.report( gsub2: ) { n.times{(asdf * str_x).gsub(/ {2,}/,    ) }}
  x.report( gsub3: ) { n.times{(asdf * str_x).gsub(/ss+/,    ) }}
  x.report( gsub4: ) { n.times{(asdf * str_x).gsub(/s{2,}/,    ) }}
end
# >>               user     system      total        real
# >> squeeze:  1.050000   0.000000   1.050000 (  1.055833)
# >> gsub1:    3.700000   0.020000   3.720000 (  3.731957)
# >> gsub2:    3.960000   0.010000   3.970000 (  3.980328)
# >> gsub3:    4.520000   0.020000   4.540000 (  4.549919)
# >> gsub4:    4.840000   0.010000   4.850000 (  4.860474)
# >> 
# >> long strings
# >>               user     system      total        real
# >> squeeze:  0.310000   0.180000   0.490000 (  0.485224)
# >> gsub1:    3.420000   0.130000   3.550000 (  3.554505)
# >> gsub2:    3.850000   0.120000   3.970000 (  3.974213)
# >> gsub3:    4.880000   0.130000   5.010000 (  5.015750)
# >> gsub4:    5.310000   0.150000   5.460000 (  5.461797)

测试依据的是<代码>squeeze( )或gsub(>, 缩小了重复的空间。 正如我所期望的那样,蒸.( )使reg灭。 使用空间特性的登记比使用<代码><>/代码>的等值格式更快。

当然,监管机构比较灵活,但想到是否需要一个管理机构能够大大改变贵国法典的处理速度。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签