我有一个叫部门的模特。
class Department < ActiveRecord::Base
# Associations
belongs_to :client
belongs_to :facility
scope :facility_departments, ->(facility_id) {
where("facility_id = ? ", facility_id)}
scope :matching_departments, ->(facility_id, identifier) {facility_departments(facility_id).where("
? REGEXP reg_exp ", identifier.to_s).order("weight DESC") }
endfacility_id和一个正则表达式列。matching_departments (参见上文)。那么,在我的department-1表中,departments的正则表达式应该是什么?我用(^[0-9]{9,}$)更新了它--将所有数字标识符与length 9匹配。如何排除9 zeros
发布于 2016-03-17 12:00:04
它不是很漂亮,但应该与最简单的regex风格一起工作。进行9次单独的测试,检查每个位置的非0位数:
^(?:[1-9]\d{8}|\d[1-9]\d{7}|\d{2}[1-9]\d{6}|\d{3}[1-9]\d{5}|\d{4}[1-9]\d{4}|\d{5}[1-9]\d{3}|\d{6}[1-9]\d{2}|\d{7}[1-9]\d|\d{8}[1-9])$第一次测试1-9在第一个位置,然后是8位数.下一个是数字,后面是1-9,然后是7位数字。等等..。
如果regex样式不支持数字字符类,请将\d替换为[0-9]。
希望这对你有用。
问候
编辑:--它甚至可能不支持非捕获组,所以用一个普通的捕获组代替它:
^([1-9][0-9]{8}|[0-9][1-9][0-9]{7}|[0-9]{2}[1-9][0-9]{6}|[0-9]{3}[1-9][0-9]{5}|[0-9]{4}[1-9][0-9]{4}|[0-9]{5}[1-9][0-9]{3}|[0-9]{6}[1-9][0-9]{2}|[0-9]{7}[1-9][0-9]|[0-9]{8}[1-9])$https://stackoverflow.com/questions/36057318
复制相似问题