0

正規表現の専門家ではないので、次のコードは最適化できると思います。助けていただければ幸いです。

line = ".ui-icon-arrowrefresh-1-s { background-position: -176px -3px; }"

line.gsub!(/-?\d+px/) do |match|
  match.gsub(/-?\d+/) do |i|
    i.to_i + 4
  end
end

#=> ".ui-icon-arrowrefresh-1-s { background-position: -172px 1px; }"
4

2 に答える 2

2
line.gsub(/-?\d+(?=px)/){|s| s.to_i+4}
于 2012-12-20T13:14:17.977 に答える
2

これについては、先読みやグループ化は本当に必要ありません。

line.gsub(/-?\d+px/){|s| "#{s.to_i+4}px"}
#=> ".ui-icon-arrowrefresh-1-s { background-position: -172px 1px; }"
于 2012-12-20T13:47:27.590 に答える