删除“@” Ruby 中的符号及其后面的所有内容

发布于 2024-11-28 14:47:48 字数 289 浏览 1 评论 0原文

我正在开发一个应用程序,我需要将用户电子邮件地址中“@”符号之前的任何内容作为他/她的名字和姓氏传递。例如,如果用户的电子邮件地址“[电子邮件受保护]”当用户提交表单时,我从电子邮件中删除“@example.com”并分配“user”作为名字和姓氏。

我做了研究,但没能找到在 Ruby 中做到这一点的方法。有什么建议吗?

I am working on an application where I need to pass on the anything before "@" sign from the user's email address as his/her first name and last name. For example if the user has an email address "[email protected]" than when the user submits the form I remove "@example.com" from the email and assign "user" as the first and last name.

I have done research but was not able to find a way of doing this in Ruby. Any suggestions ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

太阳公公是暖光 2024-12-05 14:47:48

您可以按“@”拆分并仅使用第一部分。

email.split("@")[0]

这将为您提供“@”之前的第一部分。

You can split on "@" and just use the first part.

email.split("@")[0]

That will give you the first part before the "@".

未央 2024-12-05 14:47:48

要捕获 @ 符号之前的任何内容

my_string = "[email protected]"
substring = my_string[/[^@]+/]
# => "user"

To catch anything before the @ sign:

my_string = "[email protected]"
substring = my_string[/[^@]+/]
# => "user"
野鹿林 2024-12-05 14:47:48

只需在@符号处分开并抓住它之前的内容即可。

string.split('@')[0]

Just split at the @ symbol and grab what went before it.

string.split('@')[0]
多孤肩上扛 2024-12-05 14:47:48

String#split 将会很有用。给定一个字符串和一个参数,它返回一个数组,将字符串拆分为该字符串上的单独元素。因此,如果您有:

e = [email protected]
e.split("@")
 #=> ["test", "testing.com"]

因此,您将采用 e.split("@")[0] 作为地址的第一部分。

The String#split will be useful. Given a string and an argument, it returns an array splitting the string up into separate elements on that String. So if you had:

e = [email protected]
e.split("@")
 #=> ["test", "testing.com"]

Thus you would take e.split("@")[0] for the first part of the address.

小耗子 2024-12-05 14:47:48

使用 gsub 和正则表达式

first_name = email.gsub(/@[^\s]+/,"")



irb(main):011:0> Benchmark.bmbm do |x|
irb(main):012:1* email = "[email protected]"
irb(main):013:1> x.report("split"){100.times{|n| first_name = email.split("@")[0]}}
irb(main):014:1> x.report("regex"){100.times{|n| first_name = email.gsub(/@[a-z.]+/,"")}}
irb(main):015:1> end
Rehearsal -----------------------------------------
split   0.000000   0.000000   0.000000 (  0.000000)
regex   0.000000   0.000000   0.000000 (  0.001000)
-------------------------------- total: 0.000000sec

            user     system      total        real
split   0.000000   0.000000   0.000000 (  0.001000)
regex   0.000000   0.000000   0.000000 (  0.000000)
=> [#<Benchmark::Tms:0x490b810 @label="", @stime=0.0, @real=0.00100016593933105, @utime=0.0, @cstime=0.0, @total=0.0, @cutime=0.0>, #<Benchmark::Tms:0x4910bb0 @
label="", @stime=0.0, @real=0.0, @utime=0.0, @cstime=0.0, @total=0.0, @cutime=0.0>]

use gsub and a regular expression

first_name = email.gsub(/@[^\s]+/,"")



irb(main):011:0> Benchmark.bmbm do |x|
irb(main):012:1* email = "[email protected]"
irb(main):013:1> x.report("split"){100.times{|n| first_name = email.split("@")[0]}}
irb(main):014:1> x.report("regex"){100.times{|n| first_name = email.gsub(/@[a-z.]+/,"")}}
irb(main):015:1> end
Rehearsal -----------------------------------------
split   0.000000   0.000000   0.000000 (  0.000000)
regex   0.000000   0.000000   0.000000 (  0.001000)
-------------------------------- total: 0.000000sec

            user     system      total        real
split   0.000000   0.000000   0.000000 (  0.001000)
regex   0.000000   0.000000   0.000000 (  0.000000)
=> [#<Benchmark::Tms:0x490b810 @label="", @stime=0.0, @real=0.00100016593933105, @utime=0.0, @cstime=0.0, @total=0.0, @cutime=0.0>, #<Benchmark::Tms:0x4910bb0 @
label="", @stime=0.0, @real=0.0, @utime=0.0, @cstime=0.0, @total=0.0, @cutime=0.0>]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文