Topic: regular expression

Hi im just wanting to write a regular exppression that will match either ',' or ' ' in a string

so far I am producing

str = "red, white blue"
str.split(/\s|\,/)
=> ["red", "", "white", "blue"]

Does anyone have any ideas as to how I could improve this.  Im not wanting the "" to be matched

Re: regular expression

Try this:

str = "red, white blue"
str.split(/,?\s/)
#=> ["red", "white", "blue"]

The question mark is a metacharacter that says "match that character (the comma) if it exists".

Last edited by ryanb (2006-07-10 19:42:09)

Railscasts - Free Ruby on Rails Screencasts

Re: regular expression

thanks.  really need to pull the regular expression out a bit more.

Re: regular expression

or you could use /[ ,]/