[英]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.