I have a string of emails like below
"test@test.comtest.test1@test.comtest@yahoo.co.intest1.test2@support.yahoo.com"
I want to convert this to an array of valid email addresses. I've been trying to solve this by using regex.
I have a string of emails like below
"test@test.comtest.test1@test.comtest@yahoo.co.intest1.test2@support.yahoo.com"
I want to convert this to an array of valid email addresses. I've been trying to solve this by using regex.
To sum up what everyone's been commenting,
You really need to delimit your data better. For example you might do:
test@test.com;test.test1@test.com;test@yahoo.co.in;test1.test2@support.yahoo.com
Doing this would let you split your answer on ;
to get a list of possible email addresses. However, look at this this SO accepted answer about the problem with validating email addresses using regex. There's so many formats and possibilities for email addresses that they are hard to validate with just a regex.
Here is an example of delimiting using the above string.
You may be able to do this if you can guarantee that:
If you can make some guarantees, then you can do something like this in Ruby:
emails = "test@test.comtest.test1@test.comtest@yahoo.co.intest1.test2@support.yahoo.com"
# Test for a known string ending in a known domain.
emails.scan /(test.*?[.](?:com|in))/
# Test for known domains with positive lookbehind.
emails.scan /(?<=^|com|in).*?(?:com|in)/
In other words, if it's fixture data, fix your fixtures to have a sensible delimiter. That will take less time and be less error-prone.
On the other hand, if it's real data then it's unlikely you can separate them. Distinguishing an arbitrary domain name from an arbitrary trailing mailbox name is impractical.