简体   繁体   English

协助 RUBY 代码从 email 字符串 user@domain.com 获取“用户”、“域”和“domain.com”

[英]ASSIST WITH RUBY CODE TO GET “user”, “domain” and “domain.com” from email string user@domain.com

Please I need help with something like substring of PHP for Ruby that can make me get "user" "domain" and "domain.com" in this output email "user@domain.com" Please I need help with something like substring of PHP for Ruby that can make me get "user" "domain" and "domain.com" in this output email "user@domain.com"

eg john@dortmund.com例如 john@dortmund.com

Let say I have %3% in my line of code to call user the result will be = "john".假设我的代码行中有 %3% 来调用用户,结果将是 =“john”。

Let say I have %2% in my line of code to call domainname the result will be = "dortmund".假设我的代码行中有 %2% 来调用域名,结果将是 =“dortmund”。

Let say I have %1% in my line of code to call domain.com the result will be = "dortmund.com"假设我的代码行中有 %1% 来调用 domain.com 结果将是 = "dortmund.com"

I have been cracking my head but couldn't get it.我一直在摇头,但无法得到它。

Thank you谢谢

Split is your friend!斯普利特是你的朋友!

email.split(/[@,.]/)
 => ["user", "domain", "com"]

from there you can manipulate the array to get the desired outputs.从那里您可以操纵数组以获得所需的输出。 For example:例如:

email.split(/[@,.]/).last(2).join('.')
 => "domain.com"

Some emails can have multiple dots in them and you may want to handle this:有些电子邮件中可能有多个点,您可能需要处理:

email = "some.thing@email.com"
email.split(/(.+)@(.+)\.(.+)/).drop(1)
 => ["some.thing", "email", "com"] 

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

相关问题 Rails与组织/用户相同的顶级URL路由:domain.com/user和domain.com/organization的工作原理相同 - Rails same top-level URL route for organizations/user: domain.com/user and domain.com/organization works the same 使用Heroku从domain.com设置域 - Setting up domain from domain.com with Heroku 如何在Apache上的Ruby on Rails中将domain.com更改为staging.domain.com - How to change domain.com to staging.domain.com in Ruby on Rails on Apache Nginx将subdomain.domain.com作为domain.com访问 - nginx access subdomain.domain.com as domain.com Rails / Passenger / Apache2:从www.domain.com重定向到domain.com - Rails/Passenger/Apache2: Redirect from www.domain.com to domain.com force_ssl适用于http://www.domain.com,但不适用于http://domain.com - force_ssl working for http://www.domain.com but not http://domain.com Rails:当输入为domain.com/?r=123时,在路由中重定向 - Rails: Redirect in routes when input is domain.com/?r=123 www.domain.com(而非domain.com)的Facebook无效重定向URI - Facebook Invalid Redirect URI for www.domain.com and not domain.com 在Rails中将http://domain.com/subpage重定向到http://www.domain.com/subpage - Redirect http://domain.com/subpage to http://www.domain.com/subpage in Rails 连接失败:domain.com(NoMethodError:未定义的方法“>”为true:TrueClass) - connection failed for: domain.com (NoMethodError: undefined method `>' for true:TrueClass)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM