我在ruby代码中有以下正则表达式,它捕获了字段名称
的所有字段时间戳
我需要做的是获取名称中包含时间戳的字段
代码如下:
event.to_hash.each { |k, v|
if k =~ /(^|\.)timestamp$/ and v.to_s.to_i == v
event.set(k, LogStash::Timestamp.new(Time.at(v/1000)))
end
}
答案 0 :(得分:2)
您可以使用.*
和锚点来实现:
x = %w(timestamp also_timestamp timestamp_also also_timestamp_here not_time_stamp)
=> [
"timestamp",
"also_timestamp",
"timestamp_also",
"also_timestamp_here",
"not_time_stamp"
]
x.each do |y|
puts "#{y} is timestamp" if y.match?(/\A.*timestamp.*\z/)
end;
timestamp is timestamp
also_timestamp is timestamp
timestamp_also is timestamp
also_timestamp_here is timestamp
您也可以使用String#include
在不使用正则表达式的情况下进行操作