BSG
BSG

Reputation: 1452

How do I extract strings from a string?

I have a long string, consisting of multiple sentences, of various length, divided by a "-".

I want to iterate over the string and extract everything between the -'s, preferably to an array.

From another thread I found something that gets me pretty close, but not all the way:

longString.scan( /-([^-]*)-/)

Needless to say, I am new to Ruby, and especially to RegEx.

Upvotes: 2

Views: 397

Answers (2)

Andrew Marshall
Andrew Marshall

Reputation: 96914

What's wrong with using String#split?

longString.split('-')

Upvotes: 5

J. Holmes
J. Holmes

Reputation: 18546

Why not just use string.split()?

longString.split('-');

Upvotes: 4

Related Questions