割り当てられた値を文字列で抽出したい。
"a=b xxxxxx c = d xxxxxxxxx e= f g =h"
このように REGEX を使用した IN RUBY
["a=b","c=d","e=f", "g=h"]
私が試してみました:
'a= b sadfsf c= d'.scan(/\w=(\w+)/)
割り当てられた値を文字列で抽出したい。
"a=b xxxxxx c = d xxxxxxxxx e= f g =h"
このように REGEX を使用した IN RUBY
["a=b","c=d","e=f", "g=h"]
私が試してみました:
'a= b sadfsf c= d'.scan(/\w=(\w+)/)
"a=b xxxxxx c = d xxxxxxxxx e= f g =h"
.scan(/(\w+)\s*=\s*(\w+)/).map{|kv| kv.join("=")}
# => ["a=b", "c=d", "e=f", "g=h"]
文字列を正規表現で分割し、配列に格納します
次に、= 記号の周りの空白を削除します
str = "a=b xxxxxx c = d xxxxxxxxx e= f g =h"
results = str.scan(/[\w]+\s*\=\s*[\w]+/)
results.each { |x| x.gsub!(/\s+/, "")}
s = "a=b xxxxxx c = d xxxxxxxxx e= f g =h"
s.scan(/[[:alpha:]][[:blank:]]*=[[:blank:]]*[[:alpha:]]/).map{|e| e.delete(" ")}
# => ["a=b", "c=d", "e=f", "g=h"]