I have a csv with about 24,000 lines that I need to parse in apex. I tried splitting the string by newline ('\n'), but got a limit error:
System.LimitException: Regex too complicated
I tried looping through and getting the left of the string for each newline:
pos = contents.Indexof('\n');
String this_line = contents.left(pos);
I know this will get me into limits issues, but I want to see if I can even start with the parsing.
For this method, the above snippet only finds the first newline.
In order to make sure I wasn't having dos/unix errors with newlines, I changed the csv to have the following string as line delimiter: ~~~. This did not make a difference.
Sample Content:
id,email_address,optin_status,optin_status_last_updated~~~3,082790@test.com,manually_uploaded,""~~~5,0@test.com,bounced,2011-10-31 18:10:40~~~7,101k@test.com,manually_uploaded,""~~~9,1254@test.com,bounced,2012-05-24 14:05:02~~~11,134@test.com,manually_uploaded,""~~~13,1613@test.com,manually_uploaded,""~~~15,123na@test.com,unsubscribed,2012-12-03 15:02:56~~~17,123tst@test.com,manually_uploaded,""~~~19,123test@test.com,manually_uploaded,""~~~21,12@test.com,unsubscribed,2012-11-28 22:56:58~~~23,12ser@test.com,manually_uploaded,""~~~25,152t@test.com,manually_uploaded,""~~~27,16e@test.com,manually_uploaded,""~~~29,17@test.com,manually_uploaded,""~~~31,180@test.com,manually_uploaded,""~~~33,1vz@test.com,manually_uploaded,""~~~35,1a@test.com,manually_uploaded,""~~~37,1ov@test.com,manually_uploaded,""~~~39,1d@1d.com,bounced,2012-02-10 00:32:17~~~41,1gabrman@test.com,unsubscribed,2012-01-11 20:36:35~~~43,1libbypollak@gmail.com,bounced,2012-01-23 18:02:27~~~45,1on1@aish.com,manually_uploaded,""~~~47,1stmusketeer@gmail.com,manually_uploaded,""~~~49,1tman@test.com,manually_uploaded,""~~~51,21vin@test.com,unsubscribed,2012-02-05 04:11:28~~~53,22im@test.com,manually_uploaded,""~~~
All emails have been changed to ensure privacy.
Code:
Integer count = 0;
Integer pos = 100;
while(count < 1000 && pos > 0){
pos = contents.Indexof('~~~');
String this_line = contents.left(pos);
lines.add(this_line);
contents = contents.remove(this_line);
System.debug('the current pos: ' + pos + ' and the new position will be: '
+ contents.IndexOf('~~~'));
count = count + 1;
}
Debug Log:
21:22:25.134 (1134910000)|USER_DEBUG|[57]|DEBUG|the current pos: 55 and the new position will be: 0
21:22:25.135 (1135010000)|USER_DEBUG|[57]|DEBUG|the current pos: 0 and the new position will be: 0
21:22:25.135 (1135081000)|USER_DEBUG|[61]|DEBUG|the number of lines: 2
Does anyone have any ideas here?
Thank you!