2016-03-28 99 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"}