Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何使用正则表达式获取架构版本?_Ruby_Regex - Fatal编程技术网

Ruby 如何使用正则表达式获取架构版本?

Ruby 如何使用正则表达式获取架构版本?,ruby,regex,Ruby,Regex,我有以下案文: # encoding: UTF-8 # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schem

我有以下案文:

# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160405090205) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"
我需要得到20160405090205号码。我怎样才能得到它


我尝试了
/version.[0-9]/
,但这只捕获版本:20160405090205您可以获得一个捕获组

string=“ActiveRecord::Schema.define(版本:20160405090205)do”
version=string.match(/version:(\d+)/m).captures.first

您可以将其设置为一个捕获组

string=“ActiveRecord::Schema.define(版本:20160405090205)do”
version=string.match(/version:(\d+)/m).captures.first
version,*.=…
在这里是必需的,因为
captures
即使只有一次捕获也会返回一个数组

version,*.=…
在这里是必需的,因为
captures
即使只有一次捕获也会返回一个数组

schema = File.read(Rails.root.join('db', 'schema.rb'))
version, *_ = schema.match(/version: (\d+)/m).captures
version # => "20160405090205"