Parse email addresses for from and to fields in Ruby
Yes, there's a gem for this; it's called mail.
require 'mail'
addresses = []
raw_addresses = Mail::AddressList.new("Abe Allen <a@a.com>, Bob Blue <b@b.com>, c@c.com")
raw_addresses.addresses.each do |a|
address = {}
address[:address] = a.address
address[:name] = a.display_name if a.display_name.present?
addresses << address
end
Ruby/Rails Parsing Emails
I've coded so that it will work even if you have an entry like: John Bob Smith Doe <bob@smith.com>
It would retrieve:
{:email => "bob@smith.com", :fname => "John", :lname => "Bob Smith Doe" }
def parse_emails(emails)
valid_emails, invalid_emails = [], []
unless emails.nil?
emails.split(/, ?/).each do |full_email|
unless full_email.blank?
if index = full_email.index(/\<.+\>/)
email = full_email.match(/\<.*\>/)[0].gsub(/[\<\>]/, "").strip
name = full_email[0..index-1].split(" ")
fname = name.first
lname = name[1..name.size] * " "
else
email = full_email.strip
#your choice, what the string could be... only mail, only name?
end
email = email.delete("<").delete(">")
email_address = EmailVeracity::Address.new(email)
if email_address.valid?
valid_emails << { :email => email, :lname => lname, :fname => fname}
else
invalid_emails << { :email => email, :lname => lname, :fname => fname}
end
end
end
end
return valid_emails, invalid_emails
end
How can I extract an email address from a string using Ruby on Rails?
Answer found in a Java post but translated to rails it would be :
string = Fashion business\n\ud83c\udf38 DM for collab\n\ud83c\udf38email.adresse@gmail.com\n\ud83c\udf
email = string.scan(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i)
email = email.adresse@gmail.com
Thanks for you answers
Mail gem. Extract recipient display name and address as separate values
Figured it out, sorry. For anyone else who hits this thread looking for the solution:
mail[:to].display_names.first
Extracting email addresses in an html block in ruby/rails
Another option if lookbehind doesn't work:
/\b(mailto:)?([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})\b/i
This would match all emails, then you can manually check if first captured group is "mailto:" then skip this match.
extract all email addresses from some .txt documents using ruby
Depending on the nature of your .txt documents, you don't have to use one of the complicated regexes that attempt to validate email addresses. You're not trying to validate anything. You're just trying to grab what's already there. Generally speaking, a regex to grab what's already there can be much simpler than a regex that needs to validate input.
An important question is whether your .txt documents contain @ signs that are not part of an email address you want to extract.
This regex handles your first two requirements:
\w+@[\w.-]+|\{(?:\w+, *)+\w+\}@[\w.-]+
Or if you want to allow any sequence of non-space characters containing an @ sign, plus your second requirement (which has spaces):
\S+@\S+|\{(?:\w+, *)+\w+\}@[\w.-]+
Extract emails from comma separated file using ruby
Looks like that is a CSV file, you could parse it like this.
require 'csv'
csv_text = File.read('input.csv')
csv = CSV.parse(csv_text, headers: true)
file = File.open("output.csv", "w")
csv.each do |row|
file.write("#{row['email']}\n")
end
Parse text email addresses using XPath, NOT //A[startswith(@href, 'mailto:')]
I would like to select a path to element that contains @ inside
Use:
//*[contains(., '@')]
It seems to me that what you actually wanted is to select elements that have a text-node child that contains "@". If this is so, use:
//*[contains(text(), '@')]
XSLT - based verification:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select=
"//*[contains(text(), '@')] "/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the following XML document:
<html>
<body>
<a href="xxx.com">xxx.com</a>
<span>someone@xxx.com</span>
</body>
</html>
the XPath expression is evaluated and the selected nodes are copied to the output:
<span>someone@xxx.com</span>
Regex to split email address between two characters @ and .
Answer to the question in the post (before it was edited)
Judging from your code, it seems like you want to extract the top level domain (although it contradicts with the title, which does not make sense). Assuming so, this will give you the top level domain.
"user@company_name.com"[/[^.]+\z/]
# => "com"Solution to a different problem that the OP additionally mentions in the comment to this answer
"user@company_name.com"[/(?<=@)[^.]+/]
# => "company_name"
Related Topics
Create Custom HTML Helpers in Ruby on Rails
Rails Carrierwave Base64 Image Upload
How to Downgrade from Ruby 1.9.2 to Ruby 1.8.7 to Run Rails 2.0.2
How to Find Each Instance of a Class in Ruby
Ruby: Intersection Between Two Ranges
Undefined Method 'Model_Name' for Project:Class
How to Match Full Words and Not Substrings in Ruby
Why Is Rails Outputting My Array
Reading and Updating Yaml File by Ruby Code
How to Use a Variable as Object Attribute in Rails
How to Read a File from Bottom to Top in Ruby
Ruby Minitest: Suite- or Class- Level Setup
Heroku App Crash H10 - Bash: Bin/Rails: No Such File or Directory
Ruby - Send Get Request with Headers
Why Can't I Use an Integer as a Key Using the New Ruby 1.9.2 Hash Syntax