nadine1988
nadine1988

Reputation: 167

How to split string with two or more separators

I have a string that's '30.04/2012', and I want to split it so the output is ['30', '04', '2012']. That's essentially x.split('.') and x.split('/'). How can I do this efficiently?

Upvotes: 3

Views: 102

Answers (2)

Joshua Cheek
Joshua Cheek

Reputation: 31786

x = "30.04/2012"
x.scan /\d+/ # => ["30", "04", "2012"]

Upvotes: 2

sawa
sawa

Reputation: 168269

Use a regex with alternatives.

x.split(/[.\/]/)

Upvotes: 7

Related Questions