0

We have a CSV file. A specific column was to have many entries and those were to be separated with a newline character "\n", which didn't happen. Sample data of the affected column are as follows.

30 units - R135                 50 units - R225                  80 units - R360

Is there a method to split this into three columns? The number of columns is not fixed. I guess we will have to use many space characters (more than two) to split entries. I need:

data = "that long string"
# Some operation
result = ["30 units - R135", "50 units - R225", "80 units - R360"]

I tried some thing like this. But it didn't work. The result was the same string.

a.split('/(\s){3,}/')

What approach could be taken to split the data?

10
  • 2
    It works for me if you get rid of the single quotes and make it a regex literal. My guess is that ruby is interpreting your regex as a literal string. Try a.split(/(\s){3,}/) instead. Commented Aug 3, 2013 at 15:32
  • 2
    You can't possibly get that result from that long string. Please show real inputs and outputs; people won't try to guess what your data looks like. Commented Aug 3, 2013 at 15:33
  • 2
    Of course it didn't have a newline, that's normally the end of record character in csv. If it was in the data, it would have to be escaped first. Commented Aug 3, 2013 at 15:47
  • 1
    @DGM: There is no "of course". Newlines are quite acceptable within quote-delimited fields. Commented Aug 3, 2013 at 16:15
  • 2
    @Jeremy: The parentheses are unnecessary in that pattern, and will cause a single space character to be stored in $1 Commented Aug 3, 2013 at 16:20

2 Answers 2

3

A statement like

a.split('/(\s){3,}/')

will split the string in a at occurrences of the string /(\s){3,}/, which (unsurprisingly) occurs nowhere in the target string, so it remains instact.

You need to specify a regexp by writing

data = '30 units - R135                 50 units - R225                  80 units - R360'

result = data.split /\s{3,}/

p result

output

["30 units - R135", "50 units - R225", "80 units - R360"]
Sign up to request clarification or add additional context in comments.

Comments

2

the correct regex for this is:

a.split(/\s{3,}/)

a nice place to try out regex expressions: http://rubular.com/ (you may not need it , but i love it too much so sharing :) )

2 Comments

The OP said he wanted to split the string on "more than two" space characters. Two is not more than two, so the pattern was correct in the first place.
@Borodin , sorry i misread the question , updated the answer accordingly.

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.