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,[email protected],manually_uploaded,""~~~5,[email protected],bounced,2011-10-31 18:10:40~~~7,[email protected],manually_uploaded,""~~~9,[email protected],bounced,2012-05-24 14:05:02~~~11,[email protected],manually_uploaded,""~~~13,[email protected],manually_uploaded,""~~~15,[email protected],unsubscribed,2012-12-03 15:02:56~~~17,[email protected],manually_uploaded,""~~~19,[email protected],manually_uploaded,""~~~21,[email protected],unsubscribed,2012-11-28 22:56:58~~~23,[email protected],manually_uploaded,""~~~25,[email protected],manually_uploaded,""~~~27,[email protected],manually_uploaded,""~~~29,[email protected],manually_uploaded,""~~~31,[email protected],manually_uploaded,""~~~33,[email protected],manually_uploaded,""~~~35,[email protected],manually_uploaded,""~~~37,[email protected],manually_uploaded,""~~~39,[email protected],bounced,2012-02-10 00:32:17~~~41,[email protected],unsubscribed,2012-01-11 20:36:35~~~43,[email protected],bounced,2012-01-23 18:02:27~~~45,[email protected],manually_uploaded,""~~~47,[email protected],manually_uploaded,""~~~49,[email protected],manually_uploaded,""~~~51,[email protected],unsubscribed,2012-02-05 04:11:28~~~53,[email protected],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!