0

Ok so i have this string

Something 06/15/2009 some other data  33 77

And I want to seperate then like this

Something
06/15/2009
some other data  
33 
77

Any idea a clean way to do this

1
  • Are there always two numbers at the end, or can there be more or less? Commented Aug 29, 2012 at 14:21

3 Answers 3

5

I would use regex (or split it by spaces?).

Here's a quick regex that split it up for you

([a-zA-Z0-9\/]*\s)

http://rubular.com/r/qWVj3yr3aw

Edit: didn't notice the one piece of data with spaces. You can still use regex you just need to clarify where one "column" of data starts and ends.

Here's an EXACT match for the data if that's what you're looking for (Although there are better ways considering this statement isn't very useful if "some other data" changes..)

(\w+)\s([\w\/]+)\s(some other data)\s\s(\d*)\s(\d*)

http://rubular.com/r/yNEBNKq97N

Sign up to request clarification or add additional context in comments.

6 Comments

@dcbarans Thanks for the rubular link, +1 - what a great tool :)
That matched on 6 or 7 different things and not the 5 that i need plus some other data are three separate matches...which will not work
@Trace You need to clarify what "some other data" is. how many words? how long can it be? Otherwise, i can't refine the query
@dcbarans - thanks for your help on this one ....i need "some other data" to be one match....so maybe we can do a regex looking for the next number and break there....not sure if that is possible
this did the trick /(.+?)\s+(\d+\/\d+\/\d+)\s+(.+?)\s+(\d+)\s+(\d+)/
|
1

You could try:

/(.+?)\s+(\d+\/\d+\/\d+)\s+(.+?)\s+(\d+)\s+(\d+)/

Rubular example

Gives what you want for the example given at least.

Update:

It might be better to make the .+ parts greedy:

/(.+)\s+(\d+\/\d+\/\d+)\s+(.+)\s+(\d+)\s+(\d+)/

This way, 'something' or 'some other data' can include dates and numbers.

Comments

0

A simple way to do it is some_string.split(" "), to get an array of the substrings.

"Something 06/15/2009 some other data  33 77".split(" ")
=> ["Something", "06/15/2009", "some", "other", "data", "33", "77"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.