Ruby:将分隔符与正则表达式匹配

Ruby:将分隔符与正则表达式匹配,ruby,regex,Ruby,Regex,我试图用一个正则表达式模式来解决这个问题,即使我的测试通过了这个解决方案,我还是希望在数组中只包含[“1”,“2”]。有更好的方法吗 内部评级法测试: s = "//;\n1;2" # when given a delimiter of ';' s2 = "1,2,3" # should read between commas s3 = "//+\n2+2" # should read between delimiter of '+' s.split(/[,\n]|[^0-9]/)

我试图用一个正则表达式模式来解决这个问题,即使我的测试通过了这个解决方案,我还是希望在数组中只包含
[“1”,“2”]
。有更好的方法吗

内部评级法测试

 s = "//;\n1;2" # when given a delimiter of ';'

 s2 = "1,2,3" # should read between commas

 s3 = "//+\n2+2" # should read between delimiter of '+'

 s.split(/[,\n]|[^0-9]/)

 => ["", "", "", "", "1", "2"]
module StringCalculator
  def self.add(input)
    solution = input.scan(/\d+/).map(&:to_i).reduce(0, :+)
    input.end_with?("\n") ? nil : solution
  end
end
context 'when given a newline delimiter' do                                                                                                                          
  it 'should read between numbers' do                                                                                                                              
    expect(StringCalculator.add("1\n2,3")).to eq(6)                                                                                                                 
  end                                                                                                                                                               

  it 'should not end in a newline' do                                                                                                                               
    expect(StringCalculator.add("1,\n")).to be_nil                                                                                                                 
  end                                                                                                                                                              
end 

context 'when given different delimiter' do                                                                                                                         
  it 'should support that delimiter' do                                                                                                                           
    expect(StringCalculator.add("//;\n1;2")).to eq(3)                                                                                                             
  end                                                                                                                                                          
end 
制作

 s = "//;\n1;2" # when given a delimiter of ';'

 s2 = "1,2,3" # should read between commas

 s3 = "//+\n2+2" # should read between delimiter of '+'

 s.split(/[,\n]|[^0-9]/)

 => ["", "", "", "", "1", "2"]
module StringCalculator
  def self.add(input)
    solution = input.scan(/\d+/).map(&:to_i).reduce(0, :+)
    input.end_with?("\n") ? nil : solution
  end
end
context 'when given a newline delimiter' do                                                                                                                          
  it 'should read between numbers' do                                                                                                                              
    expect(StringCalculator.add("1\n2,3")).to eq(6)                                                                                                                 
  end                                                                                                                                                               

  it 'should not end in a newline' do                                                                                                                               
    expect(StringCalculator.add("1,\n")).to be_nil                                                                                                                 
  end                                                                                                                                                              
end 

context 'when given different delimiter' do                                                                                                                         
  it 'should support that delimiter' do                                                                                                                           
    expect(StringCalculator.add("//;\n1;2")).to eq(3)                                                                                                             
  end                                                                                                                                                          
end 
测试

 s = "//;\n1;2" # when given a delimiter of ';'

 s2 = "1,2,3" # should read between commas

 s3 = "//+\n2+2" # should read between delimiter of '+'

 s.split(/[,\n]|[^0-9]/)

 => ["", "", "", "", "1", "2"]
module StringCalculator
  def self.add(input)
    solution = input.scan(/\d+/).map(&:to_i).reduce(0, :+)
    input.end_with?("\n") ? nil : solution
  end
end
context 'when given a newline delimiter' do                                                                                                                          
  it 'should read between numbers' do                                                                                                                              
    expect(StringCalculator.add("1\n2,3")).to eq(6)                                                                                                                 
  end                                                                                                                                                               

  it 'should not end in a newline' do                                                                                                                               
    expect(StringCalculator.add("1,\n")).to be_nil                                                                                                                 
  end                                                                                                                                                              
end 

context 'when given different delimiter' do                                                                                                                         
  it 'should support that delimiter' do                                                                                                                           
    expect(StringCalculator.add("//;\n1;2")).to eq(3)                                                                                                             
  end                                                                                                                                                          
end 
使用非常简单:

/\d/
-一个数字字符(
[0-9]

注意:

如果您有如下字符串,则应使用
/\d+/

s = "//;\n11;2"
s.scan(/\d+/) # => ["11", "2"]

您得到的数据看起来像以下字符串:
/1\n212

如果将数据作为文件获取,则将其视为两行。如果它是一个字符串,那么再次将其视为两个独立的行。不管是哪种情况,看起来都是这样

//1
212
当输出

如果是字符串:

input = "//1\n212".split("\n")
delimiter = input.first[2] # => "1"
values = input.last.split(delimiter) # => ["2", "2"]
如果是文件:

line = File.foreach('foo.txt')
delimiter = line.next[2] # => "1"
values = line.next.chomp.split(delimiter) # => ["2", "2"]

我在第一次循环时就这样做了,但问题是我需要实际读取这些字符之间的数字。我觉得我在作弊,哈哈。老实说,在
^0-9
处拆分与扫描
0-9
;-)几乎相同@p11y号。。看到这个
s=“/;\n11;2”s.split(/[^0-9]/)#=>[,,,,,,,,“11”,“2”]
,那么你也需要做额外的工作,清除
。我只是觉得我正在远离阅读分隔符。这个问题要求我能够读取任何给定分隔符之间的数字。在本例中,它是一个
。如何删除这些空字符串?字符串是否总是以
//开头\n
?这是什么意思?当你要更改你的帖子时,写一个词“编辑”,然后放入新内容。不要移除第一个帖子。如果你删除了别人会认为我愚蠢的内容,希望你能理解。明白了,我对这个问题有了更多的见解@aruprakshit
“1,2,3”的预期输出
?一旦我有了定界符,我就这样链接它
solution=input.split(/[,\n]/).map(&:to_i).reduce(0,:+)
我建议尝试使用模式拆分字符串会导致解决方案过于复杂<代码>扫描
,使用
\d+
是更好的方法,然后确定操作员是什么。那篇文章是关于使用尽可能简单的模式;很多时候,人们试图用它们做太多的事情,并且用他们无法维护的图案或有洞的图案将自己绘制到一个角落,从而导致返回错误的值。您正在尝试使用复杂模式进行拆分。相反,
scan
使用一个简单的方法。我用class方法
add()
编写的模块只传递了一个字符串。即使规范中没有涉及到这一点,但我希望涵盖所有端部。我没有为这个问题编写测试,但我认为当遇到这种情况时,我可能会出错。谢谢你的洞察力。