What I'm looking to do is split data from string into an array.
Here's the general idea of the text format...
xxxxx denotes any mix of alpha-numeric-whitespace data.
xxxxx
1 xxxxxxxxxx
2 xxxxxxxxxx
xxxxxxxxx
xxxxxxxxx
xxxxxxxx
3 xxxxxxxxxx
4 xxxxxxxxxx
xxxxxxxxxx
5 xxxxxxxxxx
(When numbers get into the double digits, the ten's place goes into the blank position in-front of the number)
Now what I want to do is have an array of 5 elements (in this case), which stores the number and all data that trails (including the new lines). In the past this was not a big deal and I could use string.split("\n") , but now I need to delimit based on some sort of regex like /\n [0-9]{1,2}/ so I'm looking for a quick and easy way to do this (as split() doesn't support regex).
I want the array to be like
array[1] = " 1 xxxxxxxxxx"
array[2] = " 2 xxxxxxxxxxx\nxxxxxxxxxx\nxxxxxxxxxx"
array[3] = " 3 xxxxxxxxxx"
...etc
split() does support regexes. Try this:
text.split(/\n(?=[1-9 ][0-9] )/)
You can use lookahead and split on (?= [1-9] |[1-9][0-9] ), perhaps anchored at the beginning of a line, but there may be issues with ambiguities in the xxxx part. This also doesn't ensure that the numbering is sequential.
var text =
"preface\n" +
" 1 intro\n" +
" 2 body\n" +
"more body\n" +
" 3 stuff\n" +
"more stuff\n" +
"even 4 stuff\n" +
"10 conclusion\n" +
"13 appendix\n";
print(text.split(/^(?= [1-9] |[1-9][0-9] )/m));
The output is (as seen on ideone.com):
preface
, 1 intro
, 2 body
more body
, 3 stuff
more stuff
even 4 stuff
,10 conclusion
,13 appendix
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With