[英]Rails + REGEX: Extract name from email domain
I'm creating a new record in Company table based on the logic below: 我正在根据以下逻辑在Company表中创建新记录:
if company.nil?
Company.create!(name: User.get_company_name(self.email)), domain: User.get_domain(self.email))
end
I want to extract the name of the domain to place in the Company table. 我想提取要放置在公司表中的域的名称。 Ie. 就是 If I have nick@nike.com, I want to use regular expressions to substitute in 'Nike'
as the company name. 如果我有nick@nike.com,我想使用正则表达式替换'Nike'
作为公司名称。 Or, in the case of ryan@ryandrake.com, I'd want the company name to be 'Ryandrake'
. 或者,对于ryan@ryandrake.com,我希望公司名称为'Ryandrake'
。
I already have this method I am using to extract the domain, but haven't managed to edit it to extract the name: 我已经有用于提取域的此方法,但尚未设法对其进行编辑以提取名称:
def self.get_domain(email_address)
email_address.gsub(/.+@([^.]+.+)/, '\1') // Returns 'nike.com' or 'ryandrake.com'
end
Any help with modifying the method above to just return 'Nike'
or 'Ryandrake'
would be super helpful! 修改上述方法以仅返回'Nike'
或'Ryandrake'
任何帮助将非常有帮助!
Put the capturing group only to capture the word chars present next to @
. 将捕获组仅用于捕获@
旁边的字符char。 .+
inside the capturing group helps to capture also the .com
part also. 捕获组中的.+
还可帮助捕获.com
部分。
email_address.gsub(/.+@([^.]+).+/, '\1')
or 要么
email_address.gsub(/.+@([^.]+)\..+/, '\1')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.