编辑 2:这是一个简洁的通用解决方案,基于已编辑的问题:
# Replace %foo in format string with value of calling obj.foo
def custom_format(format, obj)
format.gsub(/%([a-z]\w*)/i){ |s| obj.respond_to?($1) ? obj.send($1) : s }
end
formatter = "DATA OBJECT %id
%name
Created on %date
See %url for more %infoz
We are %pct% done."
# Create a simple class with accessor methods for these attributes
DataObject = Struct.new(:id,:name,:date,:url,:pct)
data = DataObject.new(123456,"Important data","1/1/1","www.url.com",57)
formatted = custom_format(formatter,data)
puts formatted
#=> DATA OBJECT 123456
#=> Important data
#=> Created on 1/1/1
#=> See www.url.com for more %infoz
#=> We are 57% done
该正则表达式允许%x
, %xyzzy
, 甚至%F13
and %z_x_y
。它允许用户在%
任何地方使用文字,只要它后面没有已知值。
请注意,如果您的对象没有访问器方法,您可以改为使用:
# Replace %foo in format string with value @foo inside obj
# If the value is `nil` or `false` the original placeholder will be used
def custom_format(format, obj)
format.gsub(/%([a-z]\w*)/i){ |s| obj.instance_variable_get( :"@#{$1}" ) || s }
end
…但是直接进入对象的实例变量可能不是最好的主意。
给定一个通用或特定的“格式化字符串”:
gs = "Hour %d, minute %d"
fs = "Hour %H, minute %M"
…您可以通过以下方式创建“格式化字符串”:
使用sprintf
或String#%
与一般字符串
s = sprintf( gs, 1, 2 ) #=> "Hour 1, minute 2"
s = gs % [1,2] #=> "Hour 1, minute 2"
使用Time#strftime
时间对象(以及正确的占位符值,根据文档):
s = Time.now.strftime(fs) #=> "Hour 10, minute 08"
…您可以通过拆分来“解析”格式化字符串%
:
pieces = gs.split(/(%[^%\s])/) #=> ["Hour ", "%d", ", minute ", "%d"]
…在大多数情况下,您通常可以使用带有此代码的格式化字符串从格式化字符串中提取值(仅经过轻微测试):
# With s="Hour 10, minute 08"
parts = s.match /\A#{fs.gsub(/%([^%\s])/o,'(?<\1>.+?)')}\z/
p parts[:H] #=> "10"
p parts[:M] #=> "08"
# If the formatting string uses the same placeholder more than once
# you will need to ask for the parts by index, not by name
parts = s.match /\A#{gs.gsub(/%([^%\s])/o,'(?<\1>.+?)')}\z/
p parts[1] #=> "10"
p parts[2] #=> "08"
那条神奇的线噪音将格式化字符串转换为一个正则表达式,该表达式捕获并命名每个占位符:
"Hour %H, minute %M"
/\AHour (?<H>.+?), minute (?<M>.+?)\z/
当MatchData
您将字符串与此正则表达式匹配时返回的值会按名称和索引跟踪所有部分。
编辑:这是一个更强大的解决方案,用于使用格式化程序扫描字符串,它处理 sprintf 格式化占位符,例如%-3d
or %0.3f
:
require 'strscan'
def scan_with_format( format, str )
s = StringScanner.new(format)
parts = []
accum = ""
until s.eos?
if (a=s.scan( /%%/ )) || (b=s.scan( /%[^a-z]*[a-z]/i ))
parts << Regexp.escape(accum) unless accum.empty?
accum = ""
parts << (a ? '%' : "(?<#{b[-1]}>.+?)")
else
accum << s.getch
end
end
parts << Regexp.escape(accum) unless accum.empty?
re = /\A#{parts.join}\z/
str.match(re)
end
在行动:
formatter = "Hour %02d, minute %d, %.3fs $%d and %0d%% done"
formatted = formatter % [1, 2, 3.4567, 8, 90 ]
#=> "Hour 01, minute 2, 3.457s $8 and 90% done"
parts = scan_with_format(formatter, formatted)
#=> #<MatchData "Hour 01, minute 2, 3.457s $8 and 90% done" d:"01" d:"2" f:"3.457" d:"8" d:"90">