I have no real previous experience using regex, just saying. I want to extract domain names from email addresses with the below format.
richardc@mydomain.com so that the regex returns just: mydomain
With an explanation of how/why it works if possible! Cheers
Here capturing (...)
the domain name in group \1
and replace the whole string with that capture, which yields the domain name only at the end.
email = 'richardc@mydomain.com'
domain = email.gsub(/.+@([^.]+).+/, '\1')
# => mydomain
.+
means any character(except \n
). So its basically matching the whole email string, and capturing the domain name using ([^.]+)
[means anything but dot]