2016-03-28 96 views
0

我需要从输入中提取参数后的所有内容。字符串内的子串

  • 输入:"-a Apple -b Ball -c Chocolate"
  • 标准:需要-c后提取的一切。

我的输出应该是Chocolate。我试过split,scan并且输出返回了两个元素。任何人都可以帮我解决这个问题吗?

此外,请求你让我知道如何处理,如果我的输入是"-a Apple -c Chocolate -b Ball"

+1

所以对于'” - 一个Apple -c巧克力-b球“,你想要''巧克力-b球”(''-c'后面的所有东西],对吧? – sawa

+0

没有。如果我的输入是“-a Apple -c Chocolate -b Ball”,我仍然只想读取-c值作为巧克力。我关心的是输入字符串中-c参数的位置。 – Venkat

+0

这不是'-c'后面的所有内容。 – sawa

回答

0

假定输入是传递到红宝石脚本的命令行参数,尝试:

ARGV[ARGV.index("-c") + 1]

说明:

ARGVarray包括传递给ruby脚本的所有参数。 Array#index返回自身中第一个对象的索引。

有关更多信息,请参阅Array#index

1

如果你真的想要的标志(-c)之后的一切:

s = "-a Apple -b Ball -c Chocolate" 
index = s.index('-c') 
everything_after = s[(index + 2)..-1] 
puts everything_after # => Chocolate 

如果要分析的参数:

需要 'optparse'

opts = OptionParser.new do |parser| 
    parser.on('-a=s') do |v| 
    end 
    parser.on('-b=s') do |v| 
    end 
    parser.on('-c=s') do |v| 
    puts "-c is #{v}" 
    end 
end 

opts.parse("-a Apple -b Ball -c Chocolate".split(/\s/)) 

(你会需要指定所有的标志,否则解析器会窒息)

或者你可以简单地匹配th使用正则表达式的e内容。 我认为你正在寻找:< ANYTHING> < FLAG> <什么,但DASH> < ANYTHING>其中< FLAG>是 '-c'

s.match(/\A.*-c\s([^-]*).*\z/) do |match| 
    p match[1] 
end 
2

可以使用OptionParser库做到这一点:

require 'optparse' 

arguments = { } 

opts = OptionParser.new do |parser| 
    parser.on('-a=s') do |v| 
    arguments[:a] = v 
    end 
    parser.on('-b=s') do |v| 
    arguments[:b] = v 
    end 
    parser.on('-c=s') do |v| 
    arguments[:c] = v 
    end 
end 

opts.parse("-a Apple -b Ball -c Chocolate".split) 

arguments 
# => {:a=>"Apple", :b=>"Ball", :c=>"Chocolate"} 

它的工作原理非常灵活,所以你可以定义很多选项以及它们是如何解释的。

0
s = "-a Apple -b Ball -c Chocolate" 

一种方法:计算索引

marker = "-c" 
s[s.index(marker)+marker.size+1..-1] 
    #=> "Chocolate" 

marker = "-b" 
s[s.index(marker)+marker.size+1..-1] 
    #=> "Ball -c Chocolate" 

marker = "-a" 
s[s.index(marker)+marker.size+1..-1] 
    #=> "Apple -b Ball -c Chocolate" 

另一种方法:使用正则表达式

`\K` in the regex below means "forget everything matched so far". 

marker = "-c" 
s[/#{marker}\s+\K.*/] 
    #=> "Chocolate" 

marker = "-b" 
s[/#{marker}\s+\K.*/] 
    #=> "Ball -c Chocolate" 

marker = "-a" 
s[/#{marker}\s+\K.*/] 
    #=> "Apple -b Ball -c Chocolate" 

考虑这些标志物之一的正则表达式。

marker = "-a" 
r =/
    #{marker} # match the contents of the variable 'marker' 
    \s+   # match > 0 whitespace chars 
    \K   # forget everything matched so far 
    .*   # match the rest of the line 
    /x   # free-spacing regex definition mode 
    #=>/
    # -a   # match the contents of the variable 'marker' 
    # \s+   # match > 0 whitespace chars 
    # \K   # forget everything matched so far 
    # .*   # match the rest of the line 
    # /x 
s[r] 
    #=> "Apple -b Ball -c Chocolate" 

但如果你真的想只是标记

之间的文本我将建立标记作为键和文本值的哈希值。首先,我们将使用下面的正则表达式来分割字符串。

r =/
     \s*  # match >= 0 spaces 
     \-  # match hypen 
     (  # begin capture group 1 
     [a-z] # match marker 
    )  # end capture group 1 
     \s* # match >= 0 spaces 
     /x  # free-spacing regex definition mode 

h = s.split(r).drop(1).each_slice(2).to_h 
    #=> {"a"=>"Apple", "b"=>"Ball", "c"=>"Chocolate"} 

有了这个散列,我们可以检索每个标记的文本。

h["a"] 
    #=> "Apple" 
h["b"] 
    #=> "Ball" 
h["c"] 
    #=> "Chocolate" 

创建哈希的步骤如下。

a = s.split(r) 
    #=> ["", "a", "Apple", "b", "Ball", "c", "Chocolate"] 

注意,通过把[a-z]捕获组内的正则表达式,"a""b""c"被包括在阵列a英寸(见String#split,第三段)

b = a.drop(1) 
    #=> ["a", "Apple", "b", "Ball", "c", "Chocolate"] 
c = b.each_slice(2) 
    #=> #<Enumerator: ["a", "Apple", "b", "Ball", "c", "Chocolate"]:each_slice(2)> 

我们可以将其转换为一个数组看到枚举c的元素:

c.to_a 
    #=> [["a", "Apple"], ["b", "Ball"], ["c", "Chocolate"]] 

最后,

c.to_h 
    #=> {"a"=>"Apple", "b"=>"Ball", "c"=>"Chocolate"}