I have a repeated string="I will email you at xxx@gmail.com"
and I want to pull out just the actual email address.
How can I pull just the email out?
Note: Ideally, I'd also like to deal with user-error where the format may be "I will email you at my email xxx@gmail.com"
. In this case, the user input "my email xxx@gmail.com" and I just want the email address.
Relatively permissive regex that will work with your example:
"I will email you at xxx@gmail.com".match(/\b\S+@\S+\b/).to_s
#=> "xxx@gmail.com"
See also this RFC-compliant regex .
You can do this:
[6] pry(main)> string="I will email you at xxx@gmail.com"
=> "I will email you at xxx@gmail.com"
[7] pry(main)> string.scan(/[A-Za-z\d_\-\.+]+@[A-Za-z\d_\-\.]+\.[A-Za-z\d_\-]+/)
=> ["xxx@gmail.com"]
[8] pry(main)> str2 = "I will email you at my email xxx@gmail.com"
=> "I will email you at my email xxx@gmail.com"
[9] pry(main)> string.scan(/[A-Za-z\d_\-\.+]+@[A-Za-z\d_\-\.]+\.[A-Za-z\d_\-]+/)
=> ["xxx@gmail.com"]
There's some good reading on this subject at " How to Find or Validate an Email Address "
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.