0

We have an email service which accepts CSV attachment and has process its values and insert it into the objects. CsvReader class stop processing lines from CSV when there is "Line Feed" present inside cell values.

enter image description here

When I use this regex in Notepad++ to clean the the CSV file and then send it to the email service then the processing works fine and all lines from csv file are processed. The notepad++ regex is this:

(,"[a-zA-Z 0-9,.:']*)([\n\r]+)([a-zA-Z 0-9,.':]*")

Then I tried to use this regex in the apex code, in the Execute Autonomous Window, but I cannot get it match the text which I could match in Notepad++. And I cannot find out what kind of regex Apex is using. This what I tried so far:

    String res ='1000000522,Lesly175,,Lastname523,10242016,,1234568411,,email_78@yahoo.com,123 Street Address,,West Hills,CA,California,US,United States,91304,,422 permanent street address,,West Hills,CA,California,US,United States,91304,Female,No,,,,U.S. Citizen,11/9/1992,United States,Yes,,,,,Yes,Spain (Castilian),,,,,,,,,,,,,,,,,,,,,,,,,Yes,,Not a member of the military,"My cumulative GPA at UCSB was a 2.98 and was not as high as it could have been, due to struggling with domestic abuse, a shoulder surgery, and neck surgery. I received over a 3.2 GPA during my last two years, and a 3.69 GPA during my last year, with a 3.89 my last quarter. This was after the surgeries and during the abuse. I continued to persevere despite those situations. They have made me a more disciplined and strong willed person, more determined than ever to succeed in USC\'s MSW program.",Yes,Yes,"I received a GPA lower than a 2.0 during my first quarter at UCSB in Fall of 2010. I was put on academic probation for Winter of 2010 and was required to pull my cumulative GPA to above a 2.0 by the end of the quarter. I met this requirement and was taken off probation. I was not able to perform to the best of my ability my first quarter, due to struggling with the end of severe depression and coming to terms with my sexuality. This showed me that I needed to keep moving forward and not give up.",,,,,,,,,,,,,,,,UNIVERSITY OF CALIFORNIA - SANTA BARBARA,SANTA BARBARA CITY COLLEGE,,,,,,,,,,,,,,reffirstname520,reffirstname523,,,,,,,,,,,,,,referencelastname523,referencelastname523,,,,,,,,,,,,,,Professor,Children\'s Service Administrator,,,,,,,,,,,,,,Yes,Yes,Yes,,,,,,,,,,,,,"Oct 19, 2016 at 8:00 PM","Oct 16, 2016 at 8:00 PM","Oct 23, 2016 at 8:00 PM",,,,,,,,,,,,,100000522,,,,,,,Native,,,,,,,,,,,,,,,English,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,UCSB,15-Jun,No,,,Yes,"CYF Children, Youth, and Families",No,Yes,"University of California, Los Angeles\r\n'+
'California State University, Northridge",No,,No,Not Applicable,,,,,,,,,,,,,,,Graduate and Professional School Fair,Other,,,,,,,,,,,,,,"Graduate Fair at the University of California, Santa Barbara \r\n'+
'Santa Barbara, 2013",Social Work,,Social Work,,I am not eligible for military benefits,No,No,No,LGBTQ,Mental Health,Child and Family Services,2 Year,3 Year,Los Angeles,,No,No,No,No,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\r\n'+
'1000000523,Lesly176,Middlename2,Lastname524,10182016,,1234568412,,email.e79@gmail.com,,,Amherst,NY,New York,US,United States,14228,,423 permanent street address,,East Meadow,NY,New York,US,United States,11554,Female,No,,,,U.S. Citizen,7/12/1995,United States,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Not a member of the military,,No,No,,,,,,,,,,,,,,,,,UNIVERSITY AT BUFFALO,,,,,,,,,,,,,,,reffirstname521,reffirstname524,,,,,,,,,,,,,,referencelastname524,referencelastname524,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,100000523,,,,,,,Native,,,,,,,,,,,,,,,English,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,'+
'1000000524,Jacqueline175,Middlename3,Lastname525,9222016,,1234568413,,email155@umbc.edu,134 Street Address,,Pikesville,MD,Maryland,US,United States,21208,,424 permanent street address,,Pikesville,MD,Maryland,US,United States,21208,Female,No,,,,U.S. Citizen,3/29/1989,United States,,,,,,,,,,,,,,,,,,,,,,,Yes,,,,,,,,,,,,,No,No,,,,,,,,,,,,,,,,,UNIVERSITY OF MARYLAND - BALTIMORE COUNTY,,,,,,,,,,,,,,,reffirstname522,reffirstname525,,,,,,,,,,,,,,referencelastname525,referencelastname525,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,100000524,,,,,,,Native,,,,,,,,,,,,,,,English,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,';



    Pattern p = Pattern.compile('(,"\\[a-zA-Z 0-9,.:\'\\]*)(\\[\r\n\\]*)(\\[a-zA-Z 0-9,.\':\\]*")');
    Matcher m = p.matcher( res );




        System.debug(LoggingLevel.ERROR,'DEBUG before while loop m.matches = ' + m.matches());
        while (m.find()) {
            res = res.replace(m.group(), m.group(1) + ' ' + m.group(3));


            System.debug(LoggingLevel.ERROR,'DEBUG group()=' + m.group());
            System.debug(LoggingLevel.ERROR, 'DEBUG group(1)=' + m.group(1));
            System.debug(LoggingLevel.ERROR, 'DEBUG group(2)=' + m.group(2));
            System.debug(LoggingLevel.ERROR, 'DEBUG group(3)=' + m.group(3));
        }


        System.debug(LoggingLevel.ERROR, 'DEBUG: ApplicationImportProcessor.cleanLfInCellCsvData res=' + res);

        CsvReader reader = new CsvReader(res, ',');
2

As per Salesforce documentation, there is a function called unescapeJava() available in String class which returns a String whose Java literals are unescaped. Literals unescaped include escape sequences for quotes (\") and control characters, such as tab (\t), and carriage return (\n). I hope this can solve your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.