Topic: Split with multiple delimiters

I need to split a string on whitespace or a '.' character.  How can I do this?  I think the solution is the split function with a regexp, but I haven't been able to make it work.

Last edited by natsibley (2007-11-13 16:57:52)

Re: Split with multiple delimiters

somestring.split(/[\s\.]/)

Split on any one of these characters (the []) any whitespace (\s) or the dot (\.) . The \s will also cause it to split on tabs and newlines.

Re: Split with multiple delimiters

dots are not escaped in [] brackets in Regular Expressions wink

Re: Split with multiple delimiters

Thanks, works perfect.  I don't need it now, but for future reference is there an escape sequence that represents only single spaces not tabs or newlines?

Last edited by natsibley (2007-11-13 17:56:36)

Re: Split with multiple delimiters

Duplex wrote:

dots are not escaped in [] brackets in Regular Expressions wink

Ach!

somestring.split(/[\s.]/)

Yea the [] indicates a character class so anything in it is just a character except control characters allowed within a []. Which IIRC is just the pipe (|).

Vielen dank!

Re: Split with multiple delimiters

natsibley wrote:

Thanks, works perfect.  I don't need it now, but for future reference is there an escape sequence that represents only single spaces not tabs or newlines?

Yes.

somestring.split(/[ .]/)

http://www.regular-expressions.info/ is a good reference.

Re: Split with multiple delimiters

Thanks this post help me allot.

i am using split(/[ ,;]/) but i need to add a new line to this.