これは1つのアイテムにのみ一致する必要がありますが、検索が空白の場合は無限にループし続けます
require 'optparse'
require 'json'
require 'pp'
require "base64"
data = 'a:92:{s:7:"key_one";s:0:"";s:7:"key_two";s:17:"http://localhost/";}'
key = "key_one"
search = ""
replace = "newstring"
num = 0
while matches = /(\"#{key}\")(\}|;)s:(\d+):\"#{search.gsub('/', '\/')}(.*?)\";/.match(data)
full_match = matches[0]
the_key = matches[1]
start_char = matches[2]
characters = matches[3]
post_domain_str = matches[4]
### Debug
num = num + 1
puts the_key + "--" + start_char + "--" + characters + "--" + post_domain_str + "\n"
###
replacement_string = "#{the_key}#{start_char}s:#{characters.to_i + replace.length - search.length}:"
if search == ""
replacement_string << "\"#{replace}\";"
else
replacement_string << "\"#{replace}#{post_domain_str}\";"
end
data.gsub!(/#{Regexp.escape(full_match)}/, replacement_string)
end