2016-03-28 19 views
0

Ich muss aus einem Eingang alles extrahieren, was nach einem Parameter ist.Teilstring in String

  • Eingang: "-a Apple -b Ball -c Chocolate"
  • Kriterien: Need alles nach -c zu extrahieren.

Meine Ausgabe sollte Chocolate sein. Ich versuchte split, und die Ausgabe gab zwei Elemente zurück. Kann mir jemand bei dieser Anforderung helfen?

Bitte, lassen Sie mich wissen, wie Sie umgehen, wenn meine Eingabe "-a Apple -c Chocolate -b Ball" ist.

+1

Also für '“ -a Apfel -c Chocolate -b Ball "', du willst '" Chocolate -b Ball "' (alles nach '-c'), oder? – sawa

+0

nein. Wenn meine Eingabe "-a Apple -c Chocolate -b Ball" ist, wollte ich immer noch nur -c Wert als Chocolate lesen. Mein Problem war die Position des Parameters -c in der Eingabezeichenfolge. – Venkat

+0

Das ist nicht alles nach '-c'. – sawa

Antwort

0

Unter der Annahme, dass die Eingabe der Zeilenargumente an ein Ruby-Skript übergeben Befehl ist, versuchen:

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

Erläuterung:

ARGV ein array ist, die alle Argumente an eine ruby Skript übergeben enthält . Array#index gibt den Index des ersten Objekts in self zurück.

Weitere Informationen finden Sie unter Array#index.

1

Wenn Sie wirklich alles nach dem Marker wollen (c):

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

Wenn Sie die Argumente analysieren wollen:

require '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/)) 

(dir müssen alle Flags angeben, sonst wird der Parser ersticken)

Oder Sie könnten einfach th passen e Inhalt mit einem Regexp. Ich glaube, Sie suchen: < ANYTHING> < FLAG> < ANYTHING BUT DASH> < ANYTHING> wo < FLAG> ist '-c'

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

Sie die OptionParser Bibliothek verwenden können, dies zu tun:

Es ist ziemlich flexibel in der Funktionsweise, so dass Sie eine Vielzahl von Optionen definieren können und wie sie interpretiert werden.

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

One way:

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" 

Eine weitere Möglichkeit, einen Index berechnen: Verwenden Sie eine regex

`\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" 

Betrachten wir die Regex für einen dieser Marker.

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" 

Aber wenn Sie wirklich wollen einfach nur den Text zwischen den Markern

Ich werde einen Hash mit Markern als Schlüssel und Text als Werte konstruieren. Zuerst werden wir die folgende Regex verwenden, um die Zeichenfolge zu teilen.

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"} 

Mit diesem Hash können wir den Text für jeden Marker abrufen.

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

Die Schritte, den Hash zu erzeugen, sind wie folgt.

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

Hinweis, dass durch [a-z] innerhalb einer Einfanggruppe in der regex Einlochen, "a", "b" und "c"a in dem Array enthalten sind. (Siehe String#split, dritter Absatz.)

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)> 

wir die Elemente des Enumerators sehen c indem sie sie in ein Array konvertieren:

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

Schließlich

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