I have two questions in order to make a custom csv importer in VF/Apex. I saw many samples out there but I'm not able to find validations samples.

1) I would like to know if is any regex to validate a generic CSV according this spec

For instance:

1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00

The amount of columns of the csv will be variable...

I know that this forum is for Apex/SFDC question and I should ask in stackoverflow.com/regex but since I have to implement this in apex, I cant use the parsers that are suggested there.

2) Also I would like to know how to validate the limit of the file/string that can be imported/parsed. Thank you!

1 Answer 1


Regex is great, but I personally don't find it the best tool for larger parsing jobs. You are likely to run into a "Regex too complicated" error going down that path.

Instead you could use an apex method to parse the CSV. I haven't tired it, but there is one listed in Parse a CSV with APEX.

If you have a larger CSV you might need to split it out into a batch. See Importing large CSV files via Batch Apex.

  • Thank you Daniel for your reply but my intention is to detect if the csv format is correct before parse it or at least detect if some line has an error, I don't see that function is detecting errors, I believe it is considering the file is well formatted.
    – jm-cloud
    Commented Jul 22, 2014 at 21:23
  • @jm-cloud I'd suggest modifying the parsing code to detect the errors as the parsing is occurring and throw an exception at that point. Commented Jul 22, 2014 at 21:25
  • Thanks, your new link looks as a better approach. You you know how can I properly handle the file size limit?
    – jm-cloud
    Commented Jul 22, 2014 at 21:32
  • @jm-cloud How do you want to handle the limit? Do you need to support csv files of a certain size? Or do you need to restrict the processing before it starts? Commented Jul 22, 2014 at 21:36
  • Sorry for the unclear question. I know that SFDC has some limits regarding the file size that can be uploaded and also when that file content is transformed to string.
    – jm-cloud
    Commented Jul 22, 2014 at 21:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .