我有以下文字:
# 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 。
答案 0 :(得分:2)
你可以把它变成一个捕获组
string = "ActiveRecord::Schema.define(version: 20160405090205) do"
version = string.match(/version: (\d+)/m).captures.first
答案 1 :(得分:2)
schema = File.read(Rails.root.join('db', 'schema.rb'))
version, *_ = schema.match(/version: (\d+)/m).captures
version # => "20160405090205"
此处需要 version, *_ = ...
,因为即使只有一次捕获,captures
也会返回一个数组。