简体   繁体   English

正则表达式电子邮件:ruby 中的数字 + 电子邮件格式

[英]Regex email : number + format of email in ruby

I have a field email in my database also call emails that should have this format : 123456789@pac.gmail.com where 123456789 should be numbers and a length of 9.我的数据库中有一个字段电子邮件也调用应该具有以下格式的电子邮件: 123456789@pac.gmail.com其中123456789应该是数字,长度为 9。

In my model, I tried to format this for my user with the following code :在我的模型中,我尝试使用以下代码为我的用户格式化:

class Email < ApplicationRecord
    EMAIL_PATTERN = /^[0-9]+/@pac.gmail.com
    LENGTH = 9
    validates :email, presence: true, format: EMAIL_PATTERN, length: LENGTH
end

But with that I have an error unexpected tIVAR, expecting end but I don't get why should I have a END in my code as I don't start any loop or something, Is anyone knows how to format the email in the right way and why I have this kind of error ?但是,我有一个unexpected tIVAR, expecting end的错误unexpected tIVAR, expecting end但我不明白为什么我的代码中应该有一个END ,因为我没有开始任何循环或其他东西,有没有人知道如何以正确的方式格式化电子邮件为什么我有这种错误?

There is no need to separately check the length;无需单独检查长度; you can do that from within your regex.你可以在你的正则表达式中做到这一点。 If I'm understanding correctly, you want to make sure there are 9 digits followed by @pac.gmail.com .如果我理解正确,您要确保有 9 位数字后跟@pac.gmail.com You can do it like this:你可以这样做:

class Email < ApplicationRecord
  EMAIL_PATTERN = /\A[0-9]{9}@pac.gmail.com\z/
  validates :email, presence: true, format: EMAIL_PATTERN
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM