regex - Ruby - split string made up of emails at space or comma


Keywords:ruby 


Question: 

I'm working with an instance method inside a class that needs to take in a string of emails and separate them at either a comma OR a space.

I keep getting one or the other to pass, but can't make it so that both the comma and space pass together. I'm trying with regex.

Thanks for your help!

def parse
  @emails.split(/,\s/)
end

example of emails list string: xyz@yahoo.com, blablabla@gmail.com luliluli@msn.com


2 Answers: 

What you need is a character set, denoted by [].

@emails.split(/[,\s]+/)

The [] say to match any character in that set. The + is there because you want to treat multiple spaces between emails as a single separator.

 
@emails.split(/\s|,/) will work // is for multiple and | is to say or